输出如下:
Hi there!
This program is a bit longer than the others.
But really it‘s only more text.
Hey, wait a minute!! What was that???
1. A bird?
2. A plane?
3. A control character?
And how will this look when it prints out?
代码的说明
这个程序看起来有点复杂,这只是因为括号内的文本字符串包含了许多转义序列。每个文本字符串都由一对双引号括起来。但这个程序只是连续调用printf()函数,说明屏幕输出是由传送给printf()函数的数据所控制。
本例通过预处理指令包含了标准库中的stdio.h 文件:
#include <stdio.h> // Include the header file for input and output
这是一个预处理指令,因为它以符号#开头。stdio.h 文件提供了使用printf()函数所需的定义。
然后,定义main()函数头,指定它返回一个整数:
int main(void)
括号中的void 关键字表示不给main()函数传递信息。下一行的大括号表示其下是函数体:
{
下一行语句调用标准库函数printf(),将“Hi there!”输出到屏幕上,接着空两行,
输出“This program is a bit”。
printf("Hi there!\n\n\nThis program is a bit");
空两行是由3 个转义序列\n 生成的。转义序列\n 会把字符显示在新行上。第一个转义序列\n 结束了包含“Hi there!”的行,之后的两个转义序列\n 生成两个空行,文本“Thisprogram is a bit”显示在第4 行上。这行代码在屏幕上生成了4 行输出。
下一个printf()生成的输出跟在上一个printf()输出的最后一个字符后面。下面的语句输出文本“ longer than the others.”,其中的第一个字符是一个空白:
printf(" longer than the others.");
这个输出跟在上一个输出的后面,紧临bit 中的t。所以在文本的开头需要一个空,否则计算机就会显示“This program is a bitlonger than the others.”,这不是我们希望的结果。
下一个语句在输出前会先换行,因为双引号中文本字符串的开头是\n:
printf("\nBut really itS only more text.\n\n\n\a\a");
显示完文本后会空两行(因为有3 个\n 转义序列),然后发出两次鸣响。下一个屏幕输出从空的第二行开始。
下一个输出语句如下:
printf("Hey, wait a minute!! What was that???\n\n");
输出文本后空一行。其后的输出在空的这行开始。
以下3 行语句各插入一个制表符,显示一个数字后,再插入另一个制表符,之后是
一些文本,结束后换行。这样,输出更容易阅读:
printf("\t1.\tA bird?\n");
printf("\t2.\rA plane?\n");
printf("\t3.\tA control character?\n");
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-32998-10.html
听着看着感动的哭了
演技也很有亮点