Cin*_*rse 2 c syntax printf semantics
考虑这个程序:
#include <stdio.h>
int main()
{
int a;
a = 16;
printf("This is the first line
this is the second line
");
}
Run Code Online (Sandbox Code Playgroud)
为什么这个程序会抛出错误?为什么不能编译成功并显示输出:
This is the first line
this is the second line
|
Run Code Online (Sandbox Code Playgroud)
符号“|” 这里表示光标闪烁,表示光标移到了下一个,暗示“第二行”后面出现了 STDOUT 中的 '\n' 字符。
.
Run Code Online (Sandbox Code Playgroud)
在 ISO C 中,字符串文字必须位于单行代码中,除非\
紧邻行尾之前有一个字符,如下所示:
#include <stdio.h>
int main()
{
int a;
a = 16;
printf("This is the first line\
this is the second line\
");
}
Run Code Online (Sandbox Code Playgroud)
但是,这将打印以下内容:
#include <stdio.h>
int main()
{
int a;
a = 16;
printf("This is the first line\
this is the second line\
");
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,缩进也被打印出来。这不是你想要的。
您可以做的是在单独的行上定义多个彼此相邻的字符串文字,并根据需要添加\n
转义序列。
#include <stdio.h>
int main()
{
int a;
a = 16;
printf(
"This is the first line\n"
"this is the second line\n"
);
}
Run Code Online (Sandbox Code Playgroud)
该程序具有所需的输出:
This is the first line this is the second line
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
194 次 |
最近记录: |