C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C solved programs » Advance C programs

C program to store time in an integer variable

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

Store Time in an Integer Variable using C program

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

#include <stdio.h>

int main()
	int hh,mm,ss;
	int time;
	printf("Enter date (hh/mm/ss) format: ");
	printf("\nEntered time is: %02d:%02d:%02d\n",hh,mm,ss);
	/*adding hh,mm,ss into time*/
	/*Range of hh,mm,ss are 0 to 59 and it can be stored
	 *in 1+1+1 = 3 bytes*/
	 time	|= (hh&0xff); /*hh storing in byte 0*/
	 time	|= (mm&0xff)<<8; /*mm storing in byte 1*/
	 time	|= (ss&0xff)<<16; /*ss storing in byte 2*/
	 printf("Time in single variable: %d [Hex: %08X] \n",time,time);
	 /*Extracting hh,mm,ss from time (an integer value)*/
	 hh	= (time &0xff);	/*hh from byte 0*/
	 mm = ((time>>8)& 0xff); /*mm from byte 1*/
	 ss = ((time>>16)&0xff); /*ss from byte 2*/
	 printf("Time after extracting: %02d:%02d:%02d\n",hh,mm,ss);
	return 0;


    Enter date (hh/mm/ss) format: 11:11:11

    Entered time is: 11:11:11
    Time in single variable: 723723 [Hex: 000B0B0B] 
    Time after extracting: 11:11:11

You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions