为什么下面程序的输出是d
?
另外,请解释一下 printf 语句中 +
的用法。
#include"stdio.h"
int main(){
int a=1;
printf("%d"+a);
return 0;
}
最佳答案
问题在于像 "%d"
这样的字符串实际上是一个数组(在本例中转换为指针)。
当您执行 "%d"+a
时,您并不是将 a
附加到字符串文字,而是获取指向该字符串的指针,添加 a
到它,并打印结果。
由于 a
为 1,因此您将在 "%d"
开始后打印一个字符,即 "d"
您的代码相当于以下内容:
#include"stdio.h"
int main(){
const char* str = "%d";
printf(str+1);
return 0;
}
关于c - 在c中格式说明符中使用 `+`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28939520/