首页
/
每日頭條
/
科技
/
簡單c語言有趣的編程代碼
簡單c語言有趣的編程代碼
更新时间:2025-07-12 15:27:19

簡單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
推荐阅读
電腦怎麼更換桌面壁紙
電腦怎麼更換桌面壁紙
1、直接右鍵點擊想要更換的圖片。選擇“設置為桌面背景”即可。2、在桌面任意空白處點擊右鍵。選擇“個性...
2025-07-12
電腦鼠标失靈怎麼辦
電腦鼠标失靈怎麼辦
1、出現問題的鼠标一般以USB接口的鼠标居多,原因當然不外乎鼠标自身和USB接口問題了,首先我們來看看是否是接口問題。電腦的USB接口不止一個,我們可以嘗試更換其他的USB接口,如果是接口問題,更換USB接口即可解決問題。2、不但USB接口有嫌疑,鼠标同樣有嫌疑,但是憑我們肉眼是無法判斷鼠标是否損壞,但并不是沒有方法判斷,我們可以更換一個新的鼠标,看看是否會出現同樣的問題,如果不會,那麼就是原來鼠
2025-07-12
生命科學專業學什麼 好就業嗎
生命科學專業學什麼 好就業嗎
在填報高考志願時,有小夥伴比較關心生命科學專業就業前景好嗎?下面是由本站編輯為大家整理的“生命科學專業學什麼好就業嗎”。生物科學專業主要學什麼課程除了公共基礎課外,生物科學專業學生的主要必修課程有:普通生物學及實驗、生物化學及實驗、分子生物學及實驗、細胞生物學及實驗、微生物學及實驗、遺傳學及實驗、基...
2025-07-12
如何查看ip
如何查看ip
1、使用Windows+R鍵打開“運行”窗口,然後輸入CMD進入命令提示窗口。進入命令窗口之後,輸入...
2025-07-12
電腦分辨率多少合适
電腦分辨率多少合适
1、一般來說筆記本電腦的分辨率與筆記本電腦的屏幕尺寸有直接關系,如果是12.1寸的小型筆記本,則隻需...
2025-07-12
Copyright 2023-2025 - www.tftnews.com All Rights Reserved