1. 程式人生 > 其它 >linux下的C語言

linux下的C語言

技術標籤:linuxc語言

測試環境

首先我們需要安裝linux系統,可以選擇虛擬機器或者雙系統來安裝,新手推薦使用ubuntu(有很好的圖形化介面,簡單容易上手)。

linux下如何執行C語言程式

C語言是面向過程的語言,特點就是快,很快!

下面是一段簡單的C語言程式

/*test.c*/
#include<stdio.h>
int main() {
    printf("Hello World!"); //列印字串Hello World!
}

在windows系統中你可能直接使用編譯軟體執行該程式

而在linux系統中首先需要進行編譯,然後再執行編譯後的檔案

$ gcc test.c -o test.out //編譯檔案
$ ./test.out //執行編譯後的檔案
/*Hello World!*/

如何使用include引入程式碼段

/*test.c*/
#include<stdio.h>
int min(int a,int b){//得到最小值
        if(a>b){
                return b;
        }
        return a;
}

int max(int a,int b){//得到最大值
        if(a<b){
                return b;
        }
return a; } int main() { int a = min(10,5);//a=5 int b = max(10,5);//b=10 printf("min=%d,max=%d\n",a,b); }
/*test1.c*/
#include<stdio.h>
int max(int a,int b){//得到最大值
        if(a<b){
                return b;
        }
        return a;
}

int main() {
        int
a = max(23,50);//a=50 printf("max=%d\n",a); }

兩個程式都用了同一個方法,而我們卻要寫兩遍!

我們要偷懶!我們是不是可以將min和max函式提取出來呢?

下面我們準備兩個檔案min.c和max.c

/*min.c*/
int min(int a,int b){//得到最小值
        if(a>b){
                return b;
        }
        return a;
}
/*max.c*/
int max(int a,int b){//得到最大值
        if(a<b){
                return b;
        }
        return a;
}

現在我們修改一下test.c和test1.c檔案

/*test.c*/
#include<stdio.h>
#include"min.c"//引入min函式
#include"max.c"//引入max函式

int main() {
        int a = min(10,5);//a=5
        int b = max(10,5);//b=10
        printf("min=%d,max=%d\n",a,b);
}
/*test1.c*/
#include<stdio.h>
#include"max.c"//引入max函式

int main() {
        int a = max(23,50);//a=50
        printf("max=%d\n",a);
}

編譯一下
在這裡插入圖片描述

並不是每個檔案都要一起編譯

之前我們引入的是max.c和min.c檔案,我門能現將這兩個檔案編譯好再引入嗎?

$ gcc -c max.c -o max.o //將max.c先進行預編譯
$ gcc -c min.c -o min.o //將min.c先進行預編譯

再準備一個頭檔案head.h

int min(int a,int b);
int max(int a,int b);

讓我們再修改一下test.c

/*test.c*/
#include<stdio.h>
#include"head.h"//引入標頭檔案

int main() {
        int a = min(10,5);//a=5
        int b = max(10,5);//b=10
        printf("min=%d,max=%d\n",a,b);
}

在這裡插入圖片描述

每部分都要去單獨編譯?要是有很多個程式程式碼段呢

機智的人已經開始用工具了,比如 Makefile

建立Makefile檔案(必須命名為Makefile)

#this is a Makefile
test.out:min.o max.o test.c
        gcc min.o max.o test.c -o test.out
min.o:min.c
        gcc -c min.c
max.o:max.c
        gcc -c max.c

使用make命令執行

安裝make(先看看有沒有自帶安裝好的)

$ sudo apt-get install make

在這裡插入圖片描述

關於讀寫流和錯誤流

我們所使用printf和scanf的是已經封裝好的,下面才是它們原來的樣子

新建檔案test.c

#include<stdio.h>
int main() {
    int a;
    fscanf(stdin,"%d",&a);//輸入
    if(a != 0) {
        fprintf(stdout,"a=%d\n",a);//輸出
        return 0;
    }else {
        fprintf(stderr,"error\n");//輸出錯誤
        return 1;
    }
}
$ echo $? //可以檢視返回值(return)

在這裡插入圖片描述

黑視窗很煩,就喜歡文字編輯(流的重定向)

新建input.txt檔案

/*input.txt*/
5
4
0

修改test.c檔案

#include<stdio.h>
int main() {
    int a;
    int flag = 1;
    while(flag){
        fscanf(stdin,"%d",&a);//輸入
        if(a != 0) {
            fprintf(stdout,"a=%d\n",a);//輸出
        }else {
            fprintf(stderr,"error\n");//輸出錯誤
            flag = 0;//更改flag,停止迴圈
        }
    }
}

在這裡插入圖片描述

使用 < 可以改變輸入流的重定向
使用 > 可以改變輸出流的重定向
/*
其中 1> 代表正常輸出
    2> 代表錯誤輸出
*/

在這裡插入圖片描述

最後說說管道(顧名思義,就是傳輸資訊用的通道)

| 就是所謂的管道符號

最簡單的例子就是查詢資訊了

比如在剛才print.txt中查詢相關資訊
在這裡插入圖片描述

$ cat print.txt//得到檔案內容
/*通過 | 管道符傳輸資訊個給grep命令查詢原資訊中符合條件的資訊*/
$ cat print.txt | grep 4

通過這些方法的靈活運用,你就可以發揮自己的想象力寫一個小工具哦,歡迎大家在評論區分享。