Constants in C
Constant in C means the content whose value does not change at the time of execution of a program.
C Constants are also like normal variables. But, the only difference is, their values can not be modified by the program once they are defined.
Constants refer to fixed values. They are also called as literals
Constants may be belonging to any of the data types.
A constant is a value or an identifier whose value cannot be altered in a program.
Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.
Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well.
Constants are treated just like regular variables except that their values cannot be modified after their definition.
Different Types of C Constants
Constant | Type of Value Stored |
---|---|
Integer Constant | Constant which stores integer value |
Floating Constant | Constant which stores float value |
Character Constant | Constant which stores character value |
String Constant | Constant which stores string value |
Declare Constant in C
We can declare constant using const variable.
Declaration | Explanation |
---|---|
const int a = 10; | read as “a is an integer which is constant” |
int const a = 10; | read as “a is a constant integer” |
#include<stdio.h>
int main()
{
int const x=10;
float const y=20.50;
char const c='w';
x=20;//not allow
y=10.40;//not allow
c='v';//not allow
return 0;
}
main.c: In function ‘main’:
main.c:8:4: error: assignment of read-only variable ‘x’
x=20;//not allow
main.c:9:4: error: assignment of read-only variable ‘y’
y=10.40;//not allow
main.c:10:4: error: assignment of read-only variable ‘c’
c=’v’;//not allow
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |