`const` — C Keyword
`const` — C Keyword
The const keyword in C: marks an object as non-modifiable.
`const` — C Keyword
The const keyword in C: marks an object as non-modifiable.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
const (C)Marks an object as non-modifiable after initialization. Attempting to write to a const-qualified object is undefined behavior.
const Type name = value;
const Type* ptr; /* pointer to const (read-only target) */
Type* const ptr; /* const pointer (fixed address) */
const Type* const ptr; /* both const */
#include <stdio.h>
#include <math.h>
const double PI = 3.14159265358979;
double circle_area(double r) {
return PI * r * r;
}
/* const pointer-to-char: string contents are read-only */
int str_len(const char* s) {
int len = 0;
while (*s++) ++len;
return len;
}
int main(void) {
/* PI = 3.0; -- error: assignment of read-only variable */
printf("%.4f\n", circle_area(5.0));
printf("%d\n", str_len("hello")); /* 5 */
return 0;
}
const in C (unlike C++) does not make a variable a compile-time constant by itself; use enum or #define for compile-time integer constants.const struct Foo* to avoid copies and prevent modification.int main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}