Home > Backend Development > C++ > Constant qualifier in C

Constant qualifier in C

王林
Release: 2023-09-21 20:45:04
forward
958 people have browsed it

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;
}
Copy after login

Output

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

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

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template