學習C語言基礎知識 | 實踐篇

C語言的數據類型

C語言的數據類型
C語言的數據類型
  • (1)輸入輸出
#include <stdio.h>

int main(int args, const char *argv){
    //單純字符串輸出
    puts("hello world");
    
    //格式化輸出
    printf("hello %s\n","alicfeng");

    //輸入
    char username[10];
    int age;
    gets(username);//很危險的做法 推薦不使用
    scanf("%d",&age);//參數-(類型,參數的地址)
    printf("%s age is %d\n",username,age);

    return 0;
}

make && run

?  demo gcc -o main demoIO.c && ./main 
demoIO.c: In function ‘main’:
demoIO.c:18:5: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
     gets(username);//很危險的做法 推薦不使用
     ^
/tmp/cct0RsLf.o:在函數‘main’中:
demoIO.c:(.text+0x49): 警告: the `gets' function is dangerous and should not be used.
hello world
hello alicfeng
alic
22
alic age is 22
  • (2)C語言方法的調用
#include <stdio.h>

// 方法的聲明和定義
int max(int a, int b) {
    return a > b ? a : b;
}

int main() {
    printf("the max is %d\n",max(10,20));
    return 0;
}

make && run

?  demo gcc -o main demoFunc.c && ./main 
the max is 20
  • (3)C語言的宏定義
#include <stdio.h>

//定義宏 編譯前已經準備好 因而速度很快
#define MATH_PI 3.14

int main(){
    printf("the PI value is %f\n",MATH_PI);
    return 0;
}

make && run

?  demo gcc -o main demoDefine.c && ./main
the PI value is 3.140000
  • (4)C語言的宏定義方法
#include <stdio.h>

//定義宏方法 對于多行可以使用反斜杠
#define MAX(A, B) A>B?A:B

int main() {
    printf("the max value is %f\n",MAX(20.4,30.8));
    return 0;
}

make && run

?  demo gcc -o main demoDefineFunc.cpp && ./main
the max value is 30.800000
  • (5)C語言的條件運算符
#include <stdio.h>

//if
void ifCondition(int score) {
    if (score >= 90) {
        printf("優秀\n");
    } else if (score >= 80) {
        printf("良好\n");
    } else if (score >= 60) {
        printf("及格\n");
    } else {
        printf("不及格\n");
    }
}

//switch
void switchCondition(int score) {
    switch (score / 10) {
        case 10:
        case 9:
            printf("優秀\n");
            break;
        case 8:
            printf("良好\n");
            break;
        case 7:
        case 6:
            printf("及格\n");
            break;
        default:
            printf("不及格\n");
            break;
    }
}

//三木運算
void simpleCondition(int score) {
    puts(score >= 60 ? "及格" : "不及格");
}

int main(){
    ifCondition(95);
    switchCondition(88);
    simpleCondition(38);
    return 0;
}

make && run

?  demo gcc -o main demoCondition.c && ./main
優秀
良好
不及格
  • (6)C語言的循環
#include <stdio.h>
int main() {
    /*第一:for*/
    for (int i = 0; i < 10; ++i) {
        printf("hello %d\n", i);
    }

    for (int i = 0; i < 10; printf("hello %d\n", i++)) {
        printf("alicfeng\n");
    }


    /*第二:while*/
//    int index = 10;
//    do {
//        printf("hello %d\n", index--);
//    } while (index > 0);


    int index=0;
    while (index<10){
        printf("hello %d\n", index++);
    }
    return 0;
}
  • (7)C語言的結構體
#include <stdio.h>

/*定義一個結構體*/
struct User{
    int age;
    char *name;
};

int main(){
    struct User user;
    user.age = 20;
    user.name = "alicfeng";

    //復制一個結構體變量 但是新的變量已經分配新的內存空間
    struct User user1 = user;
    //對user的成員改變不會影響user1的值
    user.age = 21;

    printf("%s age is %d\n",user.name,user.age);
    printf("%s age is %d\n",user1.name,user1.age);
    return 0;
}

make && run

?  demo gcc -o main demoStruct.c && ./main 
alicfeng age is 21
alicfeng age is 20
  • (8)C語言的結構體指針
#include <stdio.h>
#include <stdlib.h>

struct User{
    int age;
    char *username;
};

int main(){
    //對結構體的變量需要開辟內存空間
    struct User *user = malloc(sizeof(struct User));
    user->age = 20;
    user->username = "alicfeng";

    struct User *user1 = user;
    user->age = 22;

    printf("%s age is %d\n",user1->username,user1->age);
    return 0;
}

make && run

?  demo gcc -o main demoStructPoint.c && ./main 
alicfeng age is 22
  • (9)C語言的指針函數
#include <stdio.h>
#include <stdlib.h>

//正常函數 指針調用
void sayPointer(){
    printf("hello pointer\n");
}

//指針參數的方法
void changeValue(int *value){
    *value = 100;
}

int main(){
    //example one
    void (*p)();
    p = sayPointer;
    p();

    //example two
    int value = 0;
    changeValue(&value);
    printf("the value is %d\n",value);

    return 0;
}

make && run

?  demo gcc -o main demoFuncPointer.c && ./main
hello pointer
the value is 100
  • (10)C語言的typedef關鍵字
#include <stdio.h>

//定義一個People的類型
typedef struct {
    int age;
    char *username;
} People;

void sayHello(){
    printf("hello world\n");
}
//定義一個指針指向一個方法
typedef void (*Func)();

int main(){
    People *people;
    people->age = 22;
    people->username = "alicfeng";
    printf("%s age is %d\n",people->username,people->age);

    //example two

    Func pointer = sayHello;
    pointer();

    return 0;
}

make && run

?  demo gcc -o main demoTypedef.c && ./main
alicfeng age is 22
hello world
  • (11)C語言的頭文件
#header.h文件
#ifndef DEMO_DEMOHEADER_H
#define DEMO_DEMOHEADER_H

/**
 * PS:這里只是函數的聲明,相當于接口的方法
 */
void sayHello();

#endif //DEMO_DEMOHEADER_H
//主體文件.c

#include <stdio.h>
#include "header.h"

/**
 * 函數的定義
 */
void sayHello(){
    printf("hello world\n");
}
# mainFunc
#include <stdio.h>
#include "header.h"

int main(){
    sayHello();
    return 0;
}

make && run

?  demo gcc -o main demoHeader.c header.c && ./main
hello world
  • C語言的字符串操作
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
    /*字符串的拼接 sprintf*/
    char content[100];
    //void *memset(void *s,int c,size_t n)
    //將已開辟內存空間 s 的首 n 個字節的值設為值 c
    memset(content,0,100);

    //任意類型的值拼接成字符串
    sprintf(content,"hello %s;the value is %f or %d","alicfeng",98.5,100);
    printf("%s\n",content);

    /*memcpy用來做內存拷貝*/
    char *source = "hello world";
    char des[5];
    //開始拷貝
    memcpy(des,source+6* sizeof(char),5* sizeof(char));
    printf("%s\n",des);

    /*字符串拷貝*/
    char str1[10] = "alicfeng";
    char *str2 = malloc(10);//char str2[10];也可以
    strcpy(str2,str1);
    printf("str2 value is %s\n",str2);

    return 0;
}

make &run

?  demo gcc -o main demoString.c && ./main
hello alicfeng;the value is 98.500000 or 100
world
str2 value is alicfeng
  • C語言的文件操作
#include <stdio.h>
#include <stdlib.h>

int main() {
    /*文件的寫入 覆蓋式寫入*/
    //打開文件
    FILE *file = fopen("log", "w");
    //判斷文件是否打開失敗
    if (file != NULL) {
        for (int i = 0; i < 5; fprintf(file, "%d\n", ++i)) {
            printf("寫 %d 入成功\n", i+1);
        }
    }
    //關閉文件
    fclose(file);

    /*文件的寫入 追加式寫入*/
    FILE *fileA = fopen("log", "a+");//以追加的方式寫入
    if (NULL== fileA){
        perror("open file error");
        exit(1);
    }
    fputs("alic appending",fileA);
    fclose(fileA);

    /*文件的讀取*/
    FILE *fileR = fopen("log", "r");
    //獲取文件的大小
    //要將指針放置文件流的最末端 參數0為偏移量
    fseek(fileR, 0, SEEK_END);
    long size = ftell(fileR);
    //創建存放內容的緩沖區 并且還想在末端添加\0, 因而調大一個字節
    char buf[size + 1];
    //重新將指針置于文件的開始位置
    fseek(fileR, 0, SEEK_SET);
    fread(buf, sizeof(unsigned char), size, fileR);
    buf[size] = '\0';
    fclose(fileR);
    printf("%s\n", buf);

    /*文件重命名*/
    int result = rename("log", "data");
    printf("文件重命名%s\n", result == 0 ? "成功" : "失敗");

    /*刪除文件*/
    printf("刪除文件%s\n", remove("data") ? "失敗" : "成功");

    return 0;
}

make && run

?  demo gcc -o main demoFile.c && ./main
寫 1 入成功
寫 2 入成功
寫 3 入成功
寫 4 入成功
寫 5 入成功
1
2
3
4
5
alic appending
文件重命名成功
刪除文件成功
  • C語言小游戲
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int main(){
    //設置隨機數的機制 否則產生的隨機數是固定的值
    srand(time(NULL));
    //隨機生成兩位數的int
    int randValue = rand()%100;
    int userInput;
    printf("Hello 請輸入兩位數:\n");
    while(1){
        scanf("%d",&userInput);
        if (userInput>randValue){
            printf("數值過大\n");
        } else if (userInput<randValue){
            printf("數值過小\n");
        } else{
            printf("恭喜您,答對了!\n");
            break;
        }
    }
    return 0;
}

make && run

?  demo gcc -o main demoGame.c && ./main
Hello 請輸入兩位數:
50
數值過小
75
數值過小
85
數值過大
80
數值過小
82
數值過大
81
恭喜您,答對了!

源碼地址
****價值源于技術,貢獻源于分享****

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,250評論 6 530
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 97,923評論 3 413
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,041評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,475評論 1 308
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,253評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,801評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,882評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,023評論 0 285
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,530評論 1 331
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,494評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,639評論 1 366
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,177評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,890評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,289評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,552評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,242評論 3 389
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,626評論 2 370

推薦閱讀更多精彩內容