【发布时间】:2020-08-01 03:11:26
【问题描述】:
我想为 int age 使用占位符,但 %i 或 %s 不起作用。
这是我的代码:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int age = get_int("What is your age ?\n");
printf("You are %i years old.\n");
}
这是错误:
test1.c:7:22: error: more '%' conversions than data arguments [-Werror,-Wformat]
printf("You are %i years old.\n");
~^
1 error generated.
<builtin>: recipe for target 'test1' failed
make: *** [test1] Error 1
【问题讨论】:
-
printf("You are %i years old.\n", age); -
@M.NejatAydin 请将其转换为解释性答案。
-
在
printf()和家族中,格式化字符串中的每个%参数都需要传递一个相应的附加参数,以及该格式规范的数据。如果缺少(如此处),某些编译器会将其视为错误,有些可能会发出警告,有些则忽略(也取决于设置)。可执行的 C 代码在这方面是不可原谅的:没有编译器需要告诉你,并且代码会尝试按照它被告知的去做,无论是否出错。
标签: c printf placeholder cs50