Computer >> Computer tutorials >  >> Programming >> C programming

What are different types of constants in C language?


Constant is a value that cannot be changed during program execution; it is fixed.

In C language, a number or character or string of characters is called a constant. And it can be any data type. Constants are also called as literals.

There are two types of constants −

Primary constants − Integer, float, and character are called as Primary constants.

Secondary constants − Array, structures, pointers, Enum, etc., called as secondary constants.

Syntax

const datatype variable;

Example for Primary constants

#include<stdio.h>
int main(){
   const int height=20;
   const int base=40;
   float area;
   area=0.5 * height*base;
   printf("The area of triangle :%f", area);
   return 0;
}

Output

The area of triangle :400.000000

Example for secondary constants

include<stdio.h>
void main(){
   int a;
   int *p;
   a=10;
   p=&a;
   printf("a=%d\n",a);//10//
   printf("p=%d\n",p);//address value of p//
   *p=12;
   printf("a=%d\n",a);//12//
   printf("p=%d\n",p);//address value of p//
}

Output

a=10
p=6422036
a=12
p=6422036