Home  >  Article  >  Backend Development  >  Constant qualifier in C

Constant qualifier in C

王林
王林forward
2023-09-21 20:45:04884browse

Constant qualifier in C

We use the const qualifier to declare a variable as a constant. This means that once a variable is initialized, we cannot change its value. There are great benefits to using const. For example, if you have a constant value of PI, you don't want any part of your program to modify that value. Therefore, you should declare it as const.

Objects declared with a const-qualified type may be placed in read-only memory by the compiler, and may not be stored at all if the address of the const object is never obtained in the program. For example,

Example

#include<stdio.h>
int main() {
   const int x = 10;
   x = 12;
   return 0;
}

Output

[Error] assignment of read-only variable &#39;x&#39;

The above is the detailed content of Constant qualifier in C. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete