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

标签 c printf structure

嗨,有人可以告诉我 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/

相关文章:

将 C dll 头文件转换为 Delphi Pascal 头文件

PHP 应用程序结构/模式 - 2 个具有共享库和 Assets 的站点

printf 后将变量内容转换为指针结果为 NULL

c - printf 语句是如何解释的?

c - 将 printf 移动到不同的行会产生不同的输出? (C)

c - 从文件中读取结构内的结构

c - 使用 gets() 在结构中获取输入时出错

c - 使用第二个字段排序时 qsort 是否保留原始顺序?

c - typedef 结构命令差异

c - 错误: expected identifier or ‘(’ before ‘TOKEN’