這是我的代碼(我知道使用 %d 是錯誤的,但我不確定我應該使用什么):
#include <stdio.h>
#include <stdlib.h>
int main()
{
char charactername[] = "Ruby";
int age =18;
printf("Once upon a time there was girl named %s\n",charactername);
printf("%s was %d years old\n",charactername,age);
age =19;
int birthday = 22/07/2003;
printf("on %d she was born\n",birthday);
printf("On 22/07/2022 she will become %d",age);
return 0;
}
這是終端給我的:
從前有一個女孩叫露比
魯比 18 歲
在 0 她出生
在 22/07/2022 她將成為 19
uj5u.com熱心網友回復:
您將使用struct tm和strftimefrom的組合time.h:
#include <stdio.h>
#include <time.h>
int main( void )
{
struct tm bdate = { .tm_year=(2003 - 1900), .tm_mday = 22, .tm_mon = 6 };
char datebuf[11] = {0};
strftime( datebuf, sizeof datebuf, "%d/%m/%Y", &bdate );
printf( "bdate = %s\n", datebuf );
return 0;
}
輸出:
$ ./bdate
bdate = 22/07/2003
uj5u.com熱心網友回復:
C 中沒有內置“日期”型別。您可以將字串用于任意文本;就像是:
const char *birthday = "22/07/2003";
您可以使用%sprintf 格式列印
printf("on %s she was born\n",birthday);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/362351.html
下一篇:如何從日期跳過某些0
