赏心悦目的C语言输出
C语言的输出功能,超出你的想象,你能想到的,她基本也能做到。
刚开始还只是照抄printf语句,老是出现下面的情况:
Hello World% $
后面才晓得 printf 函数是有特殊规定字符的,比如换行的 \\n,换页的 \\f,回车的 \\r,以及制表符 \\t 等。
这次就说说是个什么意思以及如何使用。
本节尽量只使用 printf 函数,除非 有必要,尽量简洁,然后举几个用的最广的例子。
换行显示文本
printf要完成的功能就是显示文本,比如最简单的:
/*beginner/print/print1.c*/
#include <stdio.h>
int main()
{
printf(\"Hello World\\n\");
return 0;
}
比如我们想打印一首诗,原文是
Hickory, dickory, dock,
The mouse ran up the clock.
The clock struck one,
The mouse ran down,
Hickory, dickory, dock.
这个简单呀,直接输入下面的代码
/*beginner/print/print2.c*/
#include <stdio.h>
int main()
{
printf(\"Hickory, dickory, dock,\");
printf(\"The mouse ran up the clock.\");
printf(\"The clock struck one,\");
printf(\"The mouse ran down,\");
printf(\"Hickory, dickory, dock.\");
return 0;
}
输出是什么的
Hickory, dickory, dock,The mouse ran up the clock.The clock struck one,The mouse ran down,Hickory, dickory, dock.%
这就是没有添加换行符的原因,加上以后如下所示:
/*beginner/print/print3.c*/
#include <stdio.h>
int main()
{
printf(\"Hickory, dickory, dock,\\n\");
printf(\"The mouse ran up the clock.\\n\");
printf(\"The clock struck one,\\n\");
printf(\"The mouse ran down,\\n\");
printf(\"Hickory, dickory, dock.\\n\");
return 0;
}
优雅的输出如下所示:
Hickory, dickory, dock,
The mouse ran up the clock.
The clock struck one,
The mouse ran down,
Hickory, dickory, dock.
回车显示进度条效果
其实回车的意思并不是通俗意义上的回车,你敲下键盘,叫做Enter,是另外一种回车。
这里的回车是不换行从头开始的意思,是ASCII码为13的特殊字符,换行是ASCII码为10的特殊字符。
这个示例只能通过自己编译来使用了,代码简单,如下,就能看到进度条的效果了
/*beginner/print/print4.c*/
#include <stdio.h>
#include <unistd.h>
int main()
{
printf(\"* \\r\");
fflush(stdout);
sleep(1);
printf(\"***** \\r\");
fflush(stdout);
sleep(1);
printf(\"******* \\r\");
fflush(stdout);
sleep(1);
printf(\"********* \\r\");
fflush(stdout);
sleep(1);
printf(\"************* \\r\");
fflush(stdout);
sleep(1);
printf(\"***************** \\r\");
fflush(stdout);
sleep(1);
printf(\"*********************\\r\");
fflush(stdout);
sleep(1);
printf(\"\\n\\n\");
return 0;
}
运行的时候,可以看到光标在移动,这个用法我是学了2个多月才知道,悲哉!
说明:fflush是用来强行刷新的,因为如果不刷新,有的时候无法显示,另外sleep是为了演示移动效果,不然毫秒级显示完成,就看不到效果了。
优雅的对齐特性
其实想对齐,是比较简单的一件事情,直接空格多敲一些就行了,如下所示:
/*beginner/print/print5.c*/
#include <stdio.h>
#include <unistd.h>
int main()
{
printf(\"Name Age ID\\n\");
printf(\"Zhang San 16 1\\n\");
printf(\"Li Si 17 2\\n\");
printf(\"Wang Wu 18 3\\n\");
return 0;
}
输入为:
Name Age ID
Zhang San 16 1
Li Si 17 2
Wang Wu 18 3
但是,如果在我们不知道数字是多少,字符串是多少的时候怎么来做呢,就是制表符的效果了。
很简单,只要在需要分割的地方加上就可以了:
/*beginner/print/print6.c*/
#include <stdio.h>
#include <unistd.h>
int main()
{
printf(\"Name \\tAge\\tID\\n\");
printf(\"Zhang San\\t16\\t1\\n\");
printf(\"Li Si \\t17\\t2\\n\");
printf(\"Wang Wu \\t18\\t3\\n\");
return 0;
}
输入为:
Name Age ID
Zhang San 16 1
Li Si 17 2
Wang Wu 18 3
集大成
这里三个都演示下,可以通过./print_all来查看效果。
###################
The demo of \\n
###################
*
*****
*******
*
*****
*******
*********
*
*****
*******
*********
*************
*****************
*********************
*****
*****
*****
*****
*****
*****
*********************
*********************
###################
The demo of \\r
###################
*********************
###################
The demo of \\t
###################
Name Age ID
Zhang San 16 1
Li Si 17 2
Wang Wu 18 3
编译运行
还是跟前面的hello world一致,这次还是包含两类文件,一个是源码文件print.c,另外一个就是Makefile了。
Makefile如下所示,比上一个稍微复杂了些,其实不难理解,可以搜索Makefile帮助查看信息。
#beginner/print/Makefile
ALL : print1 print2 print3 print4 print5 print6 print_all
print1: print1.c
gcc -o print1 print1.c
print2: print2.c
gcc -o print2 print2.c
print3: print3.c
gcc -o print3 print3.c
print4: print4.c
gcc -o print4 print4.c
print5: print5.c
gcc -o print5 print5.c
print6: print6.c
gcc -o print6 print6.c
print_all: print_all.c
gcc -o print_all print_all.c
运行只需要输入make,然后./print就可以看到相关的输出了。