Q:

C program to store date in an integer variable

0

C program to store date in an integer variable

This program will read date in DD,MM,YYYY format and store it into an integer variable. After that we will extract date from that variable and print the date and DD, MM, YYYY format.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Store Date in an Integer Variable using C program

/*C program to store date in an integer variable.*/

#include <stdio.h>

int main()
{
    int dd, mm, yy;
    int date;

    printf("Enter date (dd/mm/yy) format: ");
    scanf("%d/%d/%d", &dd, &mm, &yy);

    printf("\nEntered date is: %02d/%02d/%04d\n", dd, mm, yy);

    /*adding dd,mm,yy into date*/
    /*an integer has 4 bytes and dd range is 1 to 31 , mm range is 1 to 12 which
     *can be stored in 1 byte, 1 byte and in rest of 2 bytes
     *we can store year.*/

    date = 0;
    date |= (dd & 0xff); /*dd storing in byte 0*/
    date |= (mm & 0xff) << 8; /*mm storing in byte 1*/
    date |= (yy & 0xffff) << 16; /*yy storing in byte 2 and 3*/

    printf("Date in single variable: %d [Hex: %08X] \n", date, date);

    /*Extracting dd,mm,yy from date (an integer value)*/

    dd = (date & 0xff); /*dd from byte 0*/
    mm = ((date >> 8) & 0xff); /*mm from byte 1*/
    yy = ((date >> 16) & 0xffff); /*yy from byte 2 and 3*/

    printf("Date after extracting: %02d/%02d/%04d\n", dd, mm, yy);

    return 0;
}

Output:

    Enter date (dd/mm/yy) format: 11/11/2011

    Entered date is: 11/11/2011
    Date in single variable: 131795723 [Hex: 07DB0B0B] 
    Date after extracting: 11/11/2011

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now