Member-only story
Constant in C With Example -
A constant in C is the unchanged value of a variable during the execution of the program. Suppose, if you declared an integer variable with a value of 10 as a constant variable and you want to change the value of that variable or assign a new value to that variable next time then you can not change it. If you change the value then it will throw an error during the execution.
A constant is declared with the help of the const keyword. Also, try to declare the constant variable with an uppercase letter.
Also read, Data Types in C with Examples
Let us take an example of a constant in C
#include <stdio.h>
int main()
{
const int WHOLENUM = 15;
WHOLENUM = 10;
printf("%d", WHOLENUM);
return 0;
}
Explanation of the above code:-
- #include <stdio.h> is a header file library by which we can run input and output functions.
- Each program in C language starts with main() or int main() function. Inside this function, C program codes are executed and it starts with a curly brace and ends with a curly brace.
- printf() function is used to print or display the output or text on the screen after program execution.
Output:-
Types of Constant in C with Example
Numeric Constants:- There are two types of numeric constants.