首页
/
每日頭條
/
科技
/
簡單c語言有趣的編程代碼
簡單c語言有趣的編程代碼
更新时间:2026-03-03 02:11:15

簡單c語言有趣的編程代碼?C語言的輸出功能,超出你的想象,你能想到的,她基本也能做到,我來為大家講解一下關于簡單c語言有趣的編程代碼?跟着小編一起來看一看吧!

簡單c語言有趣的編程代碼(c語言滿屏飄愛心代碼)1

簡單c語言有趣的編程代碼

賞心悅目的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	IDZhang San	16	1Li Si    17	2Wang 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就可以看到相關的輸出了。

Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
怎麼下載兩個微信
怎麼下載兩個微信
怎麼下載兩個微信?一個手機不用下載也可以有兩個微信方法如下:,下面我們就來聊聊關于怎麼下載兩個微信?接下來我們就一起去了解一下吧!怎麼下載兩個微信一個手機不用下載也可以有兩個微信。方法如下:在手機上找到“設置”按鈕并點擊打開。在設置頁面找到...
2026-03-03
防止奔馳發動機爆缸
防止奔馳發動機爆缸
江西撫州崇仁縣的陳先生是位生意人,為了能夠更好地開展業務,他花了幾十萬買了一台奔馳,可是讓他沒想到的是,車買回來了,卻給他帶來不少煩惱。陳先生新買了一輛奔馳SUV,開了才一個星期,他就有了滿肚子苦水。這輛奔馳suv,是陳先生花了40萬200...
2026-03-03
最傷手機的充電方式90%的人都用錯
最傷手機的充電方式90%的人都用錯
如今的生活,已經時時刻刻都離不開手機,買東西支付、出勤打卡、休閑娛樂、溝通聊天,幾乎沒有生活場景不需要手機的,一天和手機相處的時間幾乎比身邊的人還多,這是時代發展的客觀情況,也無需太過于擔心。買手機的時候,也都會偏向于買電池容量大的,省電耐...
2026-03-03
honor手機密碼忘了怎麼辦
honor手機密碼忘了怎麼辦
honor手機密碼忘了怎麼辦?EMUI5.0及以上的系統忘記密碼,隻能通過強制恢複出廠設置清除密碼這個方法會清除您設備上的數據,請謹慎操作,今天小編就來聊一聊關于honor手機密碼忘了怎麼辦?接下來我們就一起去研究一下吧!honor手機密碼...
2026-03-03
win7家庭普通版如何升級為旗艦版
win7家庭普通版如何升級為旗艦版
升級分為硬件升級和軟件升級,升級可以幫助我們提高電腦性能和易用性。今天,我就給大家分享win7升級為旗艦版的具體操作步驟,如果你也想升級為win7旗艦版,不妨一起看一看大家都知道win7面對不同的用戶分為了好幾個版本,家庭版,專業版,企業版...
2026-03-03
Copyright 2023-2026 - www.tftnews.com All Rights Reserved