简体   繁体   English

Turbo C ++中的日期结构

[英]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. 我被要求制作一个程序,以借助库函数getdate显示系统的当前日期,getdate用系统的当前日期填充日期结构* datep。 The predefined date structure in Turbo c++ is as shown below. Turbo c ++中的预定义日期结构如下所示。

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. 函数getdate在头文件DOS.H中具有如下声明。

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. 当getdate用系统的当前日期填充日期结构* datep时,我需要创建一个结构类型为struct date的结构变量,调用getdate函数并将变量的地址传递给getdate作为实际参数,然后显示该值这个结构变量的值应该给我们系统的当前日期。 The structure date has character variable da_day as one of it's member to store the day of system's current date. 结构日期具有字符变量da_day作为其成员之一,用于存储系统当前日期。

  • 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? 在这里,我的问题是如何将一个月中的某几天(通常是从1-28 / 29/30/31开始)存储在一个字符变量中?

  • 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? 在这里,我的问题是如何将一个月中的某几天(通常是从1-28 / 29/30/31开始)存储在一个字符变量中?

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 . 一个带符号或无符号的char变量可以轻松地保存一个介于031之间的“值”(十进制值,是学究的),用作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. 我们只对那里的十进制值感兴趣,因此%d将是预期的转换说明符。


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. 详细地讲,对于诸如printf()类的可变函数,所提供的参数将经过默认参数提升 注释 ,这会将所提供的char提升为int ,这非常适合%d转换说明符。

Also related, quoting the C11 , chapter §7.21.6.1, 同样相关,引用C11章节§7.21.6.1,

d,i

The int argument is converted to signed decimal in the style [−]dddd . int参数将以[−] dddd格式转换为带符号的十进制。 [....] [....]

c

If no l length modifier is present, the int argument is converted to an unsigned char , and the resulting character is written. 如果不存在l长度修饰符,则将int参数转换为unsigned char ,并写入结果字符。 [....] [....]


Note: 注意:

Quoting C11 , chapter §6.5.2.2 引用C11 ,第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. 默认参数提升是对尾随参数执行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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