Home »
Syntax References
Constant declaration syntax in C/C++ language
Learn: How to declare a constant in C/C++ programming language? Here you will find constant declaration syntax, explanation and example.
A constant is also the name of memory blocks but we cannot change the value of the constants at anytime (runtime).
Note: Declaration with storage class is discussed in variable declaration syntax in C/C++, we can also use storage class with the constants.
How to declare a constant in C/C++?
const keyword is used to declare a constant in C/C++ language, here is the syntax of constant declaration:
const data_type constant_name = value;
Here,
const is a keyword, which specifies that, constant_name is a constant and we cannot change its value.
data_type is the type of data.
constant_name is the name of constant.
value is the value of constant (while declaring a constant value must be provided).
Example:
//constant integer declaration
const int MAX_ROWS = 100;
//constant float declaration
const float PI = 3.14f;
//constant string (characters array) declaration
const char FLLE_NAME [] = "hello.txt";
Consider the program:
#include <stdio.h>
int main()
{
//constant integer declaration
const int MAX_ROWS = 100;
//constant float declaration
const float PI = 3.14f;
//constant string (characters array) declaration
const char FLLE_NAME [] = "hello.txt";
printf("Value of MAX_ROWS: %d\n",MAX_ROWS);
printf("Value of PI: %f\n",PI);
printf("Value of FILE_NAME: %s\n",FLLE_NAME);
return 0;
}
Output
Value of MAX_ROWS: 100
Value of PI: 3.140000
Value of FILE_NAME: hello.txt
Must read:
- variable declaration value in C language
- Basic data types in C language