admin 2026-09-29 00:33:34 养成攻略

C

编译器支持

语言

头文件

类型支持

程序工具

变参函数支持

错误处理

动态内存管理

字符串库

算法

数值

日期和时间工具

输入/输出支持

本地化支持

并发支持 (C11)

技术规范

符号索引

[编辑] C 语言

基本概念

关键词

预处理器

语句

表达式

初始化

声明

函数

杂项

C 的历史

技术规范

[编辑] 声明

指针

数组

enum

struct

union

位域

原子类型 (C11)

const

constexpr(C23)

volatile

restrict(C99)

对齐说明符

存储期和链接

外部和暂定定义

typedef

静态断言

属性 (C23)

[编辑]

C 类型系统 中的每个单独类型都有其几种限定版本,对应于 const、volatile,以及(对于指向对象类型的指针)restrict 这三个限定符中的一个、两个或全部三个。本页描述 const 限定符的效果。

用 const 限定类型声明的对象可能被编译器放置在只读内存中,如果程序中从未获取 const 对象的地址,则它可能根本不被存储。

任何尝试修改其类型为 const 限定的对象的行为都将导致未定义行为。

const int n = 1; // object of const-qualified type

int* p = (int*)&n;

*p = 2; // undefined behavior

const 语义仅适用于左值表达式;无论何时 const 左值表达式在不需要左值的上下文中使用,其 const 限定符都会丢失(请注意,如果存在 volatile 限定符,则不会丢失)。

指定 const 限定类型对象的左值表达式,以及指定结构体或联合体类型对象(至少包含一个 const 限定类型的成员,包括递归包含的聚合或联合体的成员)的左值表达式,都不是可修改的左值。特别是,它们不可赋值。

const int n = 1; // object of const type

n = 2; // error: the type of n is const-qualified

int x = 2; // object of unqualified type

const int* p = &x;

*p = 3; // error: the type of the lvalue *p is const-qualified

struct {int a; const int b; } s1 = {.b=1}, s2 = {.b=2};

s1 = s2; // error: the type of s1 is unqualified, but it has a const member

const 限定的结构体或联合体类型的成员,会继承其所属类型的限定(无论是使用 . 运算符还是 -> 运算符访问)。

struct s { int i; const int ci; } s;

// the type of s.i is int, the type of s.ci is const int

const struct s cs;

// the types of cs.i and cs.ci are both const int

如果一个数组类型通过使用 typedef 声明为 const 类型限定符,则该数组类型本身不是 const 限定的,但其元素类型是。

(直至 C23)

数组类型及其元素类型始终被认为是具有相同的 const 限定。

(自 C23 起)

typedef int A[2][3];

const A a = {{4, 5, 6}, {7, 8, 9}}; // array of array of const int

int* pi = a[0]; // Error: a[0] has type const int*

void *unqual_ptr = a; // OK until C23; error since C23

// Notes: clang applies the rule in C++/C23 even in C89-C17 modes

如果一个函数类型通过使用 typedef 声明为 const 类型限定符,则行为是未定义的。

在函数声明中,关键字 const 可以出现在用于声明函数参数的数组类型的方括号内。它限定了数组类型转换成的指针类型。

以下两个声明声明了相同的函数。

void f(double x[const], const double y[const]);

void f(double * const x, const double * const y);

(C99 起)

const 限定的复合字面量不一定表示不同的对象;它们可能与其他复合字面量以及碰巧具有相同或重叠表示的字符串字面量共享存储空间。

const int* p1 = (const int[]){1, 2, 3};

const int* p2 = (const int[]){2, 3, 4}; // the value of p2 may equal p1+1

_Bool b = "foobar" + 3 == (const char[]){"bar"}; // the value of b may be 1

(C99 起)

指向非 const 类型的指针可以隐式转换为指向相同或兼容类型的 const 限定版本的指针。反向转换需要强制类型转换表达式。

int* p = 0;

const int* cp = p; // OK: adds qualifiers (int to const int)

p = cp; // Error: discards qualifiers (const int to int)

p = (int*)cp; // OK: cast

请注意,指向 T 的指针的指针不可转换为指向 const T 的指针的指针;要使两种类型兼容,它们的限定符必须完全相同。

char *p = 0;

const char **cpp = &p; // Error: char* and const char* are not compatible types

char * const *pcp = &p; // OK, adds qualifiers (char* to char*const)

目录

1 关键字

2 注意

3 参考

4 参见

[编辑] 关键字

const

[编辑] 注意

C 从 C++ 采用了 const 限定符,但与 C++ 不同,C 中 const 限定类型的表达式不是常量表达式;它们不能用作 case 标签,也不能用于初始化静态和线程存储期对象、枚举器或位域大小。当它们用作数组大小时,结果数组是 VLA。

[编辑] 引用

C17 标准 (ISO/IEC 9899:2018)

6.7.3 类型限定符 (p: 87-90)

C11 标准 (ISO/IEC 9899:2011)

6.7.3 类型限定符 (p: 121-123)

C99 标准 (ISO/IEC 9899:1999)

6.7.3 类型限定符 (p: 108-110)

C89/C90 标准 (ISO/IEC 9899:1990)

6.5.3 类型限定符

[编辑] 参阅

C++ 文档,关于 cv(const 和 volatile)类型限定符