简体   繁体   English

将结构成员输入到 printf 函数和参数过多错误

[英]Inputting structure members into printf function and too many arguments error

Hi can anybody tell me the issue of what's going on in the printf function?嗨,有人可以告诉我printf函数中发生了什么问题吗? I keep getting an error of我不断收到错误

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);警告:格式 '%i' 需要类型为 'int' 的参数,但参数 2 的类型为 'char *' [-Wformat=] time2.hour, time2.min, time2.sec, time3.hour, time3.min, time3 .sec);

and then进而

warning: too many arguments for format [-Wformat-extra-args]警告:格式参数过多 [-Wformat-extra-args]

Anybody give some pointers as to how to fix?有人就如何修复提供一些指示吗? Thanks谢谢

#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;
} 

It looks like you have two string literals in your long printf statement, but the printf statement only allows one string at the beginning, and it must be the one that contains all the %.2i format placeholders.看起来你的长 printf 语句中有两个字符串文字,但 printf 语句只允许一个字符串开头,并且它必须是包含所有 %.2i 格式占位符的那个。

Right now, printf gets the first one, and then tries to use the second string as an input argument to the first.现在, printf 获取第一个字符串,然后尝试使用第二个字符串作为第一个字符串的输入参数。

Join those two strings into 1 string, and it should work.将这两个字符串连接成 1 个字符串,它应该可以工作。

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

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