gpt4 book ai didi

c - 将结构成员输入到 printf 函数中并出现参数过多错误

转载 作者:行者123 更新时间:2023-11-30 19:12:48 25 4
gpt4 key购买 nike

嗨,有人可以告诉我 printf 函数中发生的问题吗?我不断收到错误

warning: format ‘%i’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=] time2.hour, time2.min, time2.sec, time3.hour, time3.min, time3.sec);

然后

warning: too many arguments for format [-Wformat-extra-args]

有人可以指点一下如何修复吗?谢谢

#include <stdio.h>

struct time
{
int hour;
int min;
int sec;
};

int main (void)
{
struct time time1, time2, time3;
struct time elapsed_time (struct time time1, struct time time2);

printf("Enter your first time (hh:mm:ss) : ");
scanf ("%i:%i:%i", &time1.hour, &time1.min, &time1.sec);

printf("Enter your second time (hh:mm:ss) : ");
scanf ("%i:%i:%i", &time2.hour, &time2.min, &time2.sec);

time3 = elapsed_time(time1,time2);

printf("The time difference between %.2i:%.2i:%.2i & %.2i:%.2i:%.2i",
"is %.2i:%.2i:%.2i.\n", time1.hour, time1.min, time1.sec,
time2.hour, time2.min, time2.sec, time3.hour, time3.min, time3.sec);

return 0;
}

struct time elapsed_time (struct time time1, struct time time2)
{
struct time time3 = { 0, 0, 0 };

time3.hour = time2.hour - time1.hour;
time3.min = time2.min - time1.min;
time3.sec = time2.sec - time1.sec;

return time3;
}

最佳答案

看起来你的长 printf 语句中有两个字符串文字,但 printf 语句只允许在开头有一个字符串,而且它必须是包含所有 %.2i 格式占位符的字符串。

现在,printf 获取第一个字符串,然后尝试使用第二个字符串作为第一个字符串的输入参数。

将这两个字符串连接成 1 个字符串,它应该可以工作。

关于c - 将结构成员输入到 printf 函数中并出现参数过多错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36427930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com