在下面的程序中,函数String startingDate = new SimpleDateFormat("yyyy-MM-dd").format(startDate);
String startingTime = new SimpleDateFormat("hh:mm:ss").format(startTime);
的调用中缺少一个参数,这会在运行时导致不一致的行为,但在编译时不会给出警告。
printf
如何在编译时捕获此错误?
答案 0 :(得分:2)
使用gcc -Wall
编译
对于这种情况警告如:warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
答案 1 :(得分:-5)
为什么你使用“%s”?这里不需要使用“%s”,因为它用于打印字符串值。 对于整数值,您只需要“%d”。
像这样运行你的代码。
#include<stdio.>
int main()
{
int a = 1;
printf("%d: this is for test", a);
}
感谢。
[编辑]
如果传递的参数少于格式说明符,则未定义。编译器不需要在未定义的行为上产生任何错误。
虽然,大多数编译器都会这样做。例如,GCC为您的代码生成以下内容:
warning: too few arguments for format
编译时使用:
gcc -Wall -Wextra -std=c99 file.c
答案 2 :(得分:-6)
错误是因为
printf(&#34;%d:这是测试:%s&#34;,a);
您需要传递两个参数,一个到%d,另一个到%s。 因此,现在将替换为&#34;%d&#34;,您没有在%s的位置传递任何要替换的内容。