变量与数据类型

基本类型

  • 整型:charshortintlonglong long(可 signed/unsigned
  • 浮点:floatdoublelong double
  • 字符:char(本质是小整型)
#include <stdio.h>
int main(void) {
    int a = 10; unsigned int b = 20u; long long c = 10000000000LL;
    float x = 3.14f; double y = 2.71828; char ch = 'A';
    printf("sizeof(int)=%zu\n", sizeof(int));
    return 0;
}

作用域与存储期

  • 作用域:标识符可见的代码范围(块/文件/原型)。
  • 存储期:对象在内存中存在的时长
  • 自动存储期(局部变量,进入块创建/离开销毁)
  • 静态存储期(static/全局,程序开始到结束)
  • 动态存储期(malloc/free 管理)

声明与定义、初始化

  • 声明告诉编译器“有这个名字”;定义分配存储空间。
  • 初始化在定义时赋初值;未初始化的自动对象有不确定值。

常量与限定符

  • const 只读;volatile 防止优化(如硬件寄存器)。
  • 字面量:1010u10LL3.14f'A'"hello"