简体   繁体   中英

date structure in turbo c++

I am asked to make a program to display system's current date with the help of library function getdate,getdate fills in the date structure *datep with the system's current date. The predefined date structure in Turbo c++ is as shown below.

struct date {
int da_year;    /*current year */
char da_day;    /* day of the month */
char da_mon;    /* month (1=Jan) */
};

Function getdate has declaration in header file DOS.H as below.

void gatdate(struct date *datep);

As getdate fills in the date structure *datep with the system's current date,i need to make a structure variable of the type struct date,call the getdate function and pass the address of the variable to getdate as an actual argument,thereafter displaying the value of this structure variable should give us system's current date. The structure date has character variable da_day as one of it's member to store the day of system's current date.

  • Here my question is how is it possible to store days(from 1-28/29/30/31 usually) of the month in a character variable?

  • Also,printing the value of the structure variable the way below
    doesn't give system's current date correctly.

     printf("year/day/month is %d/%c/%c",a.da_year,da_day,da_mon); /* a is the structure variable of the type struct date */ 

    while the statement below gives correct date.

     printf("year/day/month is %d/%d/%d",a.da_year,da_day,da_mon); 

    Why is it so?

Here my question is how is it possible to store days(from 1-28/29/30/31 usually) of the month in a character variable?

A char variable, signed or unsigned, can easily hold a "value" ranging between 0 to 31 (decimal value, to be pedantic), to be used as date .

Also,printing the value of the structure variable the way below doesn't gives system's current date correctly.

Yes, because you are not printing the decimal value, you're trying to print the corresponding character representation which is not expected. We're only interested in the decimal value there, so %d would be the expected conversion specifier.


To elaborate, for variadic functions like printf() , the supplied arguments undergo default argument promotion note , which makes the supplied char to be promoted to int which is a perfect fit for %d conversion specifier.

Also related, quoting the C11 , chapter §7.21.6.1,

d,i

The int argument is converted to signed decimal in the style [−]dddd . [....]

c

If no l length modifier is present, the int argument is converted to an unsigned char , and the resulting character is written. [....]


Note:

Quoting C11 , chapter §6.5.2.2

[....] The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM