IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C | 学习笔记 -> 正文阅读

[C++知识库]C | 学习笔记

C | 学习笔记

C99标准

1. Hello World

#include <stdio.h> // 包含另一个文件

int main() // 入口函数
{
    printf("Hello, World!\n"); // Output: Hello, World!
    return 0;
}

2. 注释

int main()
{
    // 注释示例1
    /*注释示例2*/
    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 <stdio.h>

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 = %d\n", 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 <stdio.h>

#define MAX_AGE 200

int main()
{
    printf("%d\n", MAX_AGE); // Output: 200
}

3.3.2. const

#include <stdio.h>

int main()
{
    const int MAX_AGE = 200;
    printf("%d\n", MAX_AGE); // Output: 200
    return 0;
}

3.4. 枚举类型

后续更新…

4. 输入与输出

4.1. 输出

#include <stdio.h>

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 <stdio.h>

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 <stdio.h>

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

Output

i=1
i=2
i=3
i=4

5.2. do while

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

#include <stdio.h>

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

Output

10

6. 分支跳转

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

6.1. if

#include <stdio.h>

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 <stdio.h>

int main()
{
    int num1 = 10;
    int num2 = 11;
    int max = num1 > num2 ? num1 : num2;
    printf("%d\n", max); // Output: 11
}

6.3. switch

#include <stdio.h>

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 <stdio.h>

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

    // 跳过此处
    num++;

    a:
    printf("%d\n", num);
}

Output

10

7. 函数

7.1. 值传递

#include <stdio.h>

// 函数声明
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 <stdio.h>

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

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

void swap(int *a, int *b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

7.3. 保护参数

#include <stdio.h>

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("%d\n", result); // Output: 7
}

7.4. 可变参数

#include <stdio.h>
#include <stdarg.h>

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("%d\n", sum(2, 3, 4)); // Output: 7
}

8. 数组

注意

  • 数组地址传递

8.1. 数组声明

// 声明
// 示例1
int arr[5];
// 示例2
int arr2[] = {1, 2, 3};

8.2. 遍历

#include <stdio.h>

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

    // 遍历
    int len = sizeof(arr2) / sizeof(arr2[0]);
    int i;
    for (i = 0; i < len; i++)
    {
        printf("%d\n", arr2[i]);
    }
}

9. 指针

9.1. 地址引用示例

#include <stdio.h>

int main()
{
    int i = 1;
    int *p1 = &i;
    *p1 = 2;
    printf("%d", i); // Output: 2
}

9.2. 指针运算

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

#include <stdio.h>

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

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

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

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

    // 比较指针
    // 注意:此处0是指不相同意思
    printf("%d", p1 == p2); // Output: 0
}

9.3. 指针运算和二组数组

#include <stdio.h>

int main()
{
    int arr[3][3] = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
    };
    printf("%lu\n", sizeof(arr[0][0])); // Output: 4
    printf("arr[0]=%p arr[0][0]=%p\n", arr[0], &arr[0][0]); // Output: arr[0]=0x7ff7bdb05900 arr[0][0]=0x7ff7bdb05900
    printf("arr=%p arr+1=%p\n", arr, arr + 1); // Output: arr=0x7ff7bdb05900 arr+1=0x7ff7bdb0590c
}

10. 内存管理

#include <stdlib.h>

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 <stdlib.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>

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

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

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

    // 可变宏
    PR("yimt\n"); // Output: yimt
    PR("hello %s", "yimt\n"); // Output: hello world
    return 0;
}

13.2. include

#include <stdio.h> // 优先搜索系统目录
#include "stdlib.h" // 优先搜索当前工作目录

14. 注意

14.1. 浮点数精度变量

#include <stdio.h>

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

    return 0;
}

15. 参考

  • 《C Primer Plus》
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-21 18:44:48  更:2022-05-21 18:46:18 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/12 21:46:43-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码