Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Const Keyword in C

Constant in C

Constant variables are those variable, there value is not change during program execution. whenever you declare the constant its value remain fixed during execution of the program.If there is an attempt to change its value then there is an error in the progam.

  • A value or a keyword that has a definite set value is called a constant.
  • Value of Constant is fixed.
  • Constant can be any data types.
  • Constant is also known as Literals.
  • Constant is also the pointer.

Syntax :-

const data_type variable_name = value(optional);

Example:-

#include<stdio.h>
int main()
{
const int SIDE =20;
int Volume;
Volume=SIDE * SIDE * SIDE;
printf("the volume of cube with side : %d is %d cu. units", SIDE,Volume);
}

Output:-

the volume of cube with side :20 is 8000 cu. units

 

In C language there are two types of constant:-

  • Constant Literals
  • Constant variables

Constant Literals

Constant Literals are values that you use directly in the program. for example,

y=x+2;

In this example, 2 is constant literal. It has been directly use in the program. it can not be changed during execution in the program.

Constant Variables

you declare the constant variable like variables. The advantage of constant variable is that if you have to change constant later then you do not need to change it many places, you only change the value of constant variable so that the value is automatically change everywhere.

Types of Constant

There are 4 types of constant in C:-

  • Integral Constant
  • Floating constant
  • Character constant
  • String constant
  • Constant Keywords

Let’s discuss what are these in brief .

Integral constant

  • The constant in integer value is an integral constant.
  • Integer constant works as normal value.
  • Integer constant can be either negative and positive.
  • Range of integer constant -32768 to 32767.

example : int x=20 ; is a integral constant .

Source code:

#include<stdio.h>
int main()
{
const int num =20;
printf("integer constant value is %d", num);
return 0;
}

Output:

integer constant value is 20

Floating constant

  • A floating constant is a decimal value is called a floating constant.
  • Floating constant works as normal float variable.
  • its contains the decimal point.
  • If the value of floating constant is of integer type, it takes the decimal point.

example : 3.14; is a floating constant .

Source code:

#include<stdio.h>
int main()
{
const float num1 = 5;
const float num2 = 3.123;
printf("floating constant is %f", num1);
printf("floating constant is %f", num2);
return 0;
}

Output:

floating constant is 5
floating constant is 3.123

Character Constant

  • When character values to it then it is called a character constant.
  • Character constant works as normal variable.
  • Character constant takes only a single character.
  • Character constant is also used with escape sequences.

example :”A”; is a character constant.

Source code:

#include<stdio.h>
int main()
{
const char ch = 'S';
const char escape[] = "Hello\tWorld";
printf("character constant is %c", ch);
printf("string constant is %s",escape);
return 0;
}

Output:

character constant is S
string constant is Hello    World

String constant

  • A string constant is a string array (coming up in later lessons) having a fixed string values to it.
  • String constant takes single character and multiple characters.
  • The value of string constant is written within the double quotation mark (“”).
  • string constant is also used with escape sequences.

example : {‘b’,’o’,’o’,’k’,’s’,’\0′} .

Source Code:

#include<stdio.h>
int main()
{
const char st1[] = "S"; //string with single character
const char st2[] = "Hey Somya"; //Normal string constant
const char st3[] = "Hey\nSomya"; //string constant with escape sequences
printf("string constant with single character: %s", st1);
printf("normal string constant: %s", st2);
printf("string with escape sequences: %s", st3);
return 0;
}

Output:

string constant with single character : S
normal string constant : Hey Somya
string with escape sequences : Hey
Somya

Constant keyword

We can define a constant to a program using #define or const int or const float or const char which ever we want and that keyword (identifier) will have a fixed value and can not be changed later (remains a constant) .

Example : area of a triangle

#include<stdio.h>
#define LENGTH 10
void main()
{
const int BREADTH=5;
int area;
area=LENGTH*BREADTH;
printf("%d",area);
}

output:

50.