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: ");
	scanf("%d:%d:%d",&hh,&mm,&ss);
	
	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=0;
	 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;
}

Output

    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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.