反斜杠\在C中用不同的行写时如何连接printf字符串?

问题描述 投票:0回答:1

使用Dev C ++,我正在用C做一些有趣的事情:

#include<stdio.h>
main()
{
printf("Hello
      world );

^^^^这里我认为输出将类似于“ Hello(带空格)World”,但是错误:

C:\Users\ASUS\Documents\Dev C++ Programs\helloWorldDk.c In function 'main':
5   10  C:\Users\ASUS\Documents\Dev C++ Programs\helloWorldDk.c [Warning] missing terminating " character
5   3   C:\Users\ASUS\Documents\Dev C++ Programs\helloWorldDk.c [Error] missing terminating " character
6   8   C:\Users\ASUS\Documents\Dev C++ Programs\helloWorldDk.c [Warning] missing terminating " character
6   1   C:\Users\ASUS\Documents\Dev C++ Programs\helloWorldDk.c [Error] missing terminating " character
6   1   C:\Users\ASUS\Documents\Dev C++ Programs\helloWorldDk.c [Error] 'world' undeclared (first use in this function)
6   1   C:\Users\ASUS\Documents\Dev C++ Programs\helloWorldDk.c [Note] each undeclared identifier is reported only once for each function it appears in
7   1   C:\Users\ASUS\Documents\Dev C++ Programs\helloWorldDk.c [Error] expected ')' before '}' token
7   1   C:\Users\ASUS\Documents\Dev C++ Programs\helloWorldDk.c [Error] expected ';' before '}' token

但是当我添加一个\时就起作用了:

#include<stdio.h>
main()
{ 
printf("Hello \ 
   World);
}

没有任何警告和错误。这是什么魔法?并且还有其他足球存在吗,请让我知道。

c printf backslash
1个回答
2
投票

反斜杠具有许多特殊含义,例如转义序列表示特殊字符。

但是您发现的特殊含义是\后面紧跟换行符;这是“忽略我和换行符”。对于编译器来说,这解决了在字符串中间遇到换行符的问题。

((我刚刚发现您在最后一个"之前没有)。如果没有它,我怀疑结果是否与您所描述的相同。对于这个答案,我承认我假设存在闭市"。]] >

© www.soinside.com 2019 - 2024. All rights reserved.