Constants in C language

Constants are such entity in program whose value remain same during the entire execution of the program after being set. They are also called Literals.

  • Constants can be any of the data types like integer constant, floating constant, char or string constant.
  • Programmer use upper case naming convention to define constants as a best practice.

Syntax

const type constant_name = value;

const keyword is used to define constant in C.

Example

#include<stdio.h>

int main()
{
  const float PI = 3.14;
  const int RADIUS = 5;
  float area;
  area = PI * RADIUS * RADIUS;

  printf("The area of the circle with radius: %d is: %f sq. units.\n", RADIUS, area);

  return 0;
}

# output
The area of the circle with radius: 5 is: 78.500000 sq. units.

We have two constants in above program:

  • RADIUS: An integer constant
  • PI: A float constant

Constant Types in C

Constant Type Example
Integer Constant 5, 20, 100 etc
Real or Float Constant 3.14, 10.5 0.555 etc
Character Constant 'a', 'b', 'c', 'z' etc
String Constant "a", "a bc", "some word", "multi word word" etc
Octal Constant 011, 0234 etc
Hexadecimal Constant 0x1b, 0x55 etc

Using #define

We can also define constant using preprocessor.

The syntax is:

#define constant_name value;

Example: #define PI 3.14


Help me to improve BRG Trainings.