C Programming

Constant in C

A constant variable is such type of variable that's value can't be changed during execution of the program.

Constants in C

ConstantExample
Decimal Constant10, 20, 450 etc.
Real or Floating-point Constant10.3, 20.2, 450.6 etc.
Octal Constant021, 033, 046 etc.
Hexadecimal Constant0x2a, 0x7b, 0xaa etc.
Character Constant'a', 'b', 'x' etc.
String Constant"c", "c program", "c in javatpoint" etc.

 

Example

void main()

{

const int a=10;

printf("%d", a);

a=a+10;     // Value of "a" cannot be changed.

}

O/p.-   Compile Error.

We can define constant with two different mechanism-

1. Variable Constant

2. Symbolic Constant 

Variable Constant 

If any declare  any variable with const keyword is called variable constant. 

#include <stdio.h>   

#include <conio.h>

 

void main()

{

const int a=10;

printf("Constant value %d", a);

}

O/p:  Constant value 10

C Symbolic Constant or #define preprocessor 

The variable which is defined with the help of # define preprocessor is called symbolic constant.

#include <stdio.h>   

#define PI 3.14   

void main()

{

 printf("Value of PI is=%f",PI); 

 } 

O/p: Value of PI is= 3.14

                             C Programming