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

ConstantType of Value Stored
Integer ConstantConstant which stores integer value
Floating ConstantConstant which stores float value
Character ConstantConstant which stores character value
String ConstantConstant which stores string value

Declare Constant in C

We can declare constant using const variable.

DeclarationExplanation
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;
}
Tutorials Class - Output Window

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


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.