const Read-only type qualifier

const Read-only type qualifier

const bars lvalue modification; pointer-to-const differs from const pointer. It implies no read-only storage, and block const int cannot be a case label.

CategoryType qualifiers
KindKeyword / Statement
C standardC89 / C90

📝 Syntax

const int N = 100;
const int *p;   /* Pointer to const */
int * const q;  /* Const pointer */

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

10 20
20

📚 Related Tutorials

❓ FAQ

QWhat is the difference between const int *p and int * const p?
AWith const int *p, the pointed-to int cannot be modified through p, but p can point elsewhere. With int * const p, p itself cannot be redirected, though the non-const int it points to can still be modified. The forms can also be combined.
QCan an object be modified after casting away const?
AIf the original object was defined as const, modifying it is undefined behavior; a cast must not bypass that rule. If the original object is non-const and merely accessed through a pointer to const, another valid non-const lvalue may modify it.
QCan a local const int be used as a case label?
ANo. In C, the value of a const-qualified object is not an integer constant expression, even when its initializer is visible at compile time. Use an enumeration constant, a macro, or a direct constant expression instead.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏