学习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阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容