栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C | 学习笔记

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C | 学习笔记

C | 学习笔记

C99标准

1. Hello World
#include  // 包含另一个文件

int main() // 入口函数
{
    printf("Hello, World!n"); // Output: Hello, World!
    return 0;
}
2. 注释
int main()
{
    // 注释示例1
    
    return 0;
}
3. 变量与常量 3.1. C基本类型

数字

  • char
  • int
  • short
  • long
  • long long

浮点数

  • float
  • double
  • long double
3.2. 变量声明

变量名要求

必须是小写字母、大写字母、数字和下划线(_),第一个字母必须是字母或下划线。

变量声明

int main()
{
    int a = 1; // 示例1
    int b = 2, c = 3; // 示例2
    int d, e = 4; // 示例3
}

静态变量

#include 

void tryStatic(void);

int main()
{
    int count;
    for (count = 1; count <= 3; count++)
    {
        tryStatic();
    }
    return 0;
}

void tryStatic()
{
    int fade = 1;
    static int stay = 1; // 只在编译时初始化一次
    printf("fade = %d and stay = %dn", fade++, stay++);
}

Output

fade = 1 and stay = 1
fade = 1 and stay = 2
fade = 1 and stay = 3
3.3. 常量 3.3.1. define常量
#include 

#define MAX_AGE 200

int main()
{
    printf("%dn", MAX_AGE); // Output: 200
}
3.3.2. const
#include 

int main()
{
    const int MAX_AGE = 200;
    printf("%dn", MAX_AGE); // Output: 200
    return 0;
}
3.4. 枚举类型

后续更新…

4. 输入与输出 4.1. 输出
#include 

int main()
{
    int a = 16;
    printf("八进制: %o 十进制: %d 十六进制: %x", a, a, a); // Output: 八进制: 20 十进制: 16 十六进制: 10
    
    char c = 'a';
    printf("%c", c); // Output: a
}
4.2. 输入
#include 

int main()
{
    char c;
    printf("Please enter a character.n");
    scanf("%c", &c);
    printf("Input content: %c", c);

    return 0;
}

Output

Please enter a character.
1
Input content: 1
5. 循环 5.1. for
#include 

int main() {
    for (int i = 1; i < 5; i++) {
        printf("i=%dn", i);
    }
}

Output

i=1
i=2
i=3
i=4
5.2. do while

无论条件是否满足都会执行一次。

#include 

int main()
{
    int i = 10;
    do
    {
        printf("%d", i);
    } while (i < 5);
}

Output

10
6. 分支跳转

注意:可以使用break和continue这里不在演示

6.1. if
#include 

int main()
{
    int score = 10;
    if (score < 60)
    {
        printf("不及格");
    } else if (score < 70)
    {
        printf("及格");
    } else if (score < 90)
    {
        printf("良");
    } else
    {
        printf("优");
    }
}

Output

不及格
6.2. 三元运算符
#include 

int main()
{
    int num1 = 10;
    int num2 = 11;
    int max = num1 > num2 ? num1 : num2;
    printf("%dn", max); // Output: 11
}
6.3. switch
#include 

int main()
{
    int floor;

    printf("Input floor:");
    scanf("%d", &floor);
    switch (floor)
    {
        case 1:
            printf("去一楼");
            break;
        case 2:
            printf("去二楼");
            break;
        case 3:
            printf("去三楼");
            break;
        default:
            printf("未知楼层");
    }
}

Output

Input floor:1
去一楼
6.4. go to
#include 

int main()
{
    int num = 10;
    if (num >= 10)
    {
        goto a;
    }

    // 跳过此处
    num++;

    a:
    printf("%dn", num);
}

Output

10
7. 函数 7.1. 值传递
#include 

// 函数声明
int sum(int a, int b);

int main()
{
    int r = sum(1, 2);
    printf("%d", r); // Output: 3
}

// 函数定义
int sum(int a, int b)
{
    return a + b; // 函数返回值
}
7.2. 地址传递
#include 

void swap(int *a, int *b);

int main()
{
    int a = 1;
    int b = 2;
    swap(&a, &b);
    printf("a=%d b=%dn", a, b); // Output: a=2 b=1
}

void swap(int *a, int *b)
{
    int t = *a;
    *a = *b;
    *b = t;
}
7.3. 保护参数
#include 

int sum(const int arr[], int n)
{
    int i;
    int total = 0;
    
    // arr[0] = 1; // Output: error: read-only variable is not assignable
    for (i = 0; i < n; i++)
    {
        total += arr[i];
    }

    return total;
}

int main()
{
    int arr[3] = {1, 2, 3};
    int result = sum(arr, 3);
    printf("%dn", result); // Output: 7
}
7.4. 可变参数
#include 
#include 

int sum(int count, ...)
{
    int result = 0;
    va_list vaList;
    va_start(vaList, count); // 允许访问的参数数量

    int i;
    for (i = 0; i < count; i++)
    {
        result += va_arg(vaList, int); // 访问下一个参数
    }
    va_end(vaList); // 回收vaList空间
    return result;
}


int main()
{
    printf("%dn", sum(2, 3, 4)); // Output: 7
}
8. 数组

注意

  • 数组地址传递
8.1. 数组声明
// 声明
// 示例1
int arr[5];
// 示例2
int arr2[] = {1, 2, 3};
8.2. 遍历
#include 

int main()
{
    int arr2[] = {1, 2, 3};

    // 遍历
    int len = sizeof(arr2) / sizeof(arr2[0]);
    int i;
    for (i = 0; i < len; i++)
    {
        printf("%dn", arr2[i]);
    }
}
9. 指针 9.1. 地址引用示例
#include 

int main()
{
    int i = 1;
    int *p1 = &i;
    *p1 = 2;
    printf("%d", i); // Output: 2
}
9.2. 指针运算

这里只是演示指针加运算,还可以减运算。

#include 

int main()
{
    int arr[] = {1, 2, 3};
    int *p1 = arr;
    int *p2 = &arr[1];
    int *p3 = &arr[2];

    // 计算2个指针中间间隔
    printf("%ldn", p3 - p1); // Output: 2

    // 指针加
    printf("%dn", *(p1 + 1)); // Output: 2

    // 指针减
    printf("%dn", *(p2 - 1)); // Output: 1

    // 比较指针
    // 注意:此处0是指不相同意思
    printf("%d", p1 == p2); // Output: 0
}
9.3. 指针运算和二组数组
#include 

int main()
{
    int arr[3][3] = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
    };
    printf("%lun", sizeof(arr[0][0])); // Output: 4
    printf("arr[0]=%p arr[0][0]=%pn", arr[0], &arr[0][0]); // Output: arr[0]=0x7ff7bdb05900 arr[0][0]=0x7ff7bdb05900
    printf("arr=%p arr+1=%pn", arr, arr + 1); // Output: arr=0x7ff7bdb05900 arr+1=0x7ff7bdb0590c
}
10. 内存管理
#include 

int main()
{
    // 示例1
    // 申请内存
    double *temp = (double *) malloc(10 * sizeof(double));
    // 回收内存
    free(temp);

    // 示例2
    // 申请内存
    double *temp2 = (double *) calloc(10, sizeof(double));
    // 回收内存
    free(temp2);

    return 0;
}
11. 限定词
#include 

int main()
{
    // const
    const int i = 1;
    // i = 2; // 禁止修改

    // volatile
    // 易变的位置,不使用缓存
    volatile int loc1;

    // restrict
    // 告诉编译器,对象已经被指针所引用,不能通过除该指针外所有其他直接或间接的方式修改该对象的内容。
    int *restrict temp = (int *) malloc(10 * sizeof(int));
    free(temp);


    return 0;
}
12. 结构体
#include 
#include 

struct book
{
    char title[40];
    char author[40];
    float price;
};

int main()
{
    // 示例1
    struct book b1 = {
            .title = "Example",
            .author = "yimt",
            .price = 10.5f
    };
    printf("%s %s %f", b1.title, b1.author, b1.price); // Output: Example yimt 10.500000

    // 示例2
    struct book b2;
    strcpy(b2.title, "Example");
    strcpy(b2.author, "yimt");
    b2.price = 10.5f;
    printf("%s %s %f", b1.title, b1.author, b1.price);// Output: Example yimt 10.500000
    return 0;
}
13. 预处理 13.1. define
#include 

// 常量
#define MAX 10
// 宏
#define SQUARE(x) x*x
#define PSQR(name) printf("Hello %sn", name)
// ##运算符
#define XNAME(n) x ##n
#define PRINT_XN(n) printf("%dn", x ##n)
// 可变宏
#define PR(...) printf(__VA_ARGS__)

int main()
{
    // 宏
    printf("%dn", SQUARE(2)); // Output: 4
    PSQR("yimt"); // Output: Hello yimt

    // ##运算符
    int XNAME(1) = 10;
    int XNAME(2) = 11;
    printf("%d %dn", x1, x2); // Output: 10 11
    PRINT_XN(1); // Output: 10
    PRINT_XN(2); // Output: 11

    // 可变宏
    PR("yimtn"); // Output: yimt
    PR("hello %s", "yimtn"); // Output: hello world
    return 0;
}
13.2. include
#include  // 优先搜索系统目录
#include "stdlib.h" // 优先搜索当前工作目录
14. 注意 14.1. 浮点数精度变量
#include 

int main()
{
    float f = 0.12345678;
    printf("%f", f); // Output: 0.123457

    return 0;
}
15. 参考
  • 《C Primer Plus》
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/889814.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号