A constant is a value or variable that can't be changed in the program. The constant is an entity that refers to fixed values and it can’t be modified during program execution.

There are two simple ways in C to define constants −

Using #define preprocessor.
Using const keyword.

using #define preprocessor

Ex-
#include<stdio.h>
#include<conio.h>
#define pi 3.14
Void main()
{
   printf(“%f”,pi);
   getch();
}

using const keyword

Ex-
#include<stdio.h>
#include<conio.h>
Void main()
{
   const int a=5;
   printf(“%d”,a);
   getch();
}

Integer Constant

Ex-
#include<stdio.h>
#include<conio.h>
Void main()
{
   const int a=5;
   printf(“%d”,a);
   getch();
}

Character Constant

Ex-
#include<stdio.h>
#include<conio.h>
Void main()
{
   const char a=‘A’;
   printf(“%c”,a);
   getch();
}

Floating point Constant

Ex-
#include<stdio.h>
#include<conio.h>
Void main()
{
   const float a=3.14;
   printf(“%f”,a);
   getch();
}

String Constant

Ex-
#include<stdio.h>
#include<conio.h>
Void main()
{
   const char a[10]=“TechnoloG4world”;
   printf(“%s”,a);
   getch();
}

Watch the Video Lecture


            Previous                                                                                                                       Next