(C语言)printf打印的字符串太长了,我想分两行!

本文来自于公众号:C语言编程技术分享

一、提问

有下述C程序:

#include <stdio.h>
#include <stdlib.h>

int main()
{	
	printf("123456789012345678901234567890\n");
	system("pause");
	
    return 0;
}

printf函数要打印的字符串是“123456789012345678901234567890\n”,太长啦,可不可以分为两行写呀~~~

当然可以了!

二、知识点

C语言中,printf函数在打印很长的一行字符串时,为了美观我们可以分成两行写,第一个方法就是在字符串中间使用 \ 即可,也就是:

“123456789012345678901\

234567890\n”

那么上述C程序可以写成:

#include <stdio.h>
#include <stdlib.h>

int main()
{	
	printf("1234567890123456789\
01234567890\n");
	system("pause");
	
    return 0;
}

可以运行一把,结果如下:

 

还有第二个方法,分两行写的话每一行都用双引号括起来,就像这样:

"1234567890123456789"
"01234567890\n"

那么上述C程序就可以写成:;

#include <stdio.h>
#include <stdlib.h>

int main()
{	
	printf("1234567890123456789"
"01234567890\n");
	system("pause");
	
    return 0;
}

运行结果如下:

这个知识点就说清楚了!

 三、留个疑问

如果我这样写的话,下述C程序会有什么问题?欢迎留言讨论。

#include <stdio.h>
#include <stdlib.h>

int main()
{	
	printf("1234567890123456789\
  			01234567890\n");
	system("pause");
	
    return 0;
}