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

簡單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-06-01
晶澳科技再擴産
晶澳科技再擴産
晶澳科技再擴産?晶澳科技(002459)奉賢基地于4月16日順利完成産線改造,DeepBlue3.0Light高效組件順利下線(e公司),下面我們就來說一說關于晶澳科技再擴産?我們一起去了解并探讨一下這個問題吧!晶澳科技再擴産晶澳科技(00...
2026-06-01
影視解說剪輯流程
影視解說剪輯流程
Hello,大家好,我是沖哥,今天我跟大家分享一下如何用一部手機做影視解說啊。注意,我這裡講的是影視解說啊,不是影視剪輯,為什麼呢?因為現在影視剪輯大環境不是很支持啊,即使這部電影啊,或者說這部電視劇是你自己親自剪輯的,但是你發到平台上還是...
2026-06-01
産業發展現場會簡報
産業發展現場會簡報
近年來,江西偉隆科技有限公司堅持以創新引領發展,錨定市場需求,加快技術革新,持續推進産品轉型升級,實現企業高質量發展。記者日前走進江西偉隆科技有限公司生産車間看到,生産材料從各個工序輸入、加工,再通過流水線傳遞,最後輸出包裝成品,生産全程高...
2026-06-01
exchange備份
exchange備份
ExchangeOnline是Office365系列的基本構成部分,是全球應用最廣的企業郵件管理服務之一。鴻萌易備數據備份軟件可以執行ExchangeOnline(及ExchangeOn-Premises)的備份和恢複,通過執行定期備份,以...
2026-06-01
Copyright 2023-2026 - www.tftnews.com All Rights Reserved