C 变量后换行
C new line after variable
又是我,菜鸟程序员
在 C 中,我尝试在变量后执行 \n。
这是我的源代码:
int main() {
int xD = 21;
printf(xD\n);
}
我在编译时收到了这个:
new 1.c: In function ‘main’:
new 1.c:5:11: error: stray ‘\’ in program
printf(xd\n);
^
new 1.c:5:12: error: expected ‘)’ before ‘n’
printf(xd\n);
^
new 1.c:5:9: warning: format not a string literal and no format
arguments [-Wformat-security]
printf(xd\n);
^~
谁能帮帮我?
您错过了要格式化的字符串:
printf("%d\n",xD);
这里有两个问题:
1. printf()
期望第一个参数是指定格式的 const char*
字符串。
2. 编译器不知道如何解释字符串(引号)之外的\n
。这就是阻止它编译的原因。
即使您删除了 \n
,尝试 printf(xd)
也会要求 printf()
将 xd(整数)视为格式字符串——这对你。事实上,C 可能会尝试将 xd
隐式转换为 const char*
。您要求 printf()
将 "array of characters located at address 21" 解释为格式字符串(几乎可以肯定那里没有格式字符串)。
你真正想说的是:
printf("%d\n", xd);
printf 函数需要一个格式参数来标识变量xD 的类型。由于 xD 是一个整数,因此格式参数需要“%d”。
printf("%d\n", xD);
又是我,菜鸟程序员
在 C 中,我尝试在变量后执行 \n。
这是我的源代码:
int main() {
int xD = 21;
printf(xD\n);
}
我在编译时收到了这个:
new 1.c: In function ‘main’:
new 1.c:5:11: error: stray ‘\’ in program
printf(xd\n);
^
new 1.c:5:12: error: expected ‘)’ before ‘n’
printf(xd\n);
^
new 1.c:5:9: warning: format not a string literal and no format
arguments [-Wformat-security]
printf(xd\n);
^~
谁能帮帮我?
您错过了要格式化的字符串:
printf("%d\n",xD);
这里有两个问题:
1. printf()
期望第一个参数是指定格式的 const char*
字符串。
2. 编译器不知道如何解释字符串(引号)之外的\n
。这就是阻止它编译的原因。
即使您删除了 \n
,尝试 printf(xd)
也会要求 printf()
将 xd(整数)视为格式字符串——这对你。事实上,C 可能会尝试将 xd
隐式转换为 const char*
。您要求 printf()
将 "array of characters located at address 21" 解释为格式字符串(几乎可以肯定那里没有格式字符串)。
你真正想说的是:
printf("%d\n", xd);
printf 函数需要一个格式参数来标识变量xD 的类型。由于 xD 是一个整数,因此格式参数需要“%d”。
printf("%d\n", xD);