Constants are similar to the variables except that their value remains fixed. Therefore constants are those values which remains unchanged or unaltered throughout the program execution. Constants are also called as literals. They can be of any basic data types.

Defining a Constant

There are two way by which we can define a constant:

  1. using the #define preprocesser
  2. using the const keyword

1. Using the #define preprocesser

Syntax for defining constant using #define preprocesser
#define identifier value;

Program example using #define preprocesser directive

#include<stdio.h>
#define PI 3.14

int main()
{
    int radius = 5;
    float area;

    area = PI*radius*radius;
    printf("Area of circle with radius %d is %f\n", radius, area);
    return 0;
}

Output

Area of circle with radius 5 is 78.500000

2. Using the const keyword

Syntax for defining a constant using const keyword
const type var_name = value;

Program example using const keyword

#include<stdio.h>

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

    area = PI*radius*radius;
    printf("Area of circle with radius %d is %f\n", radius, area);
    return 0;
}

Output

Area of circle with radius 5 is 78.500000

Types of Constants

  1. Integer constants

    Integer constans are the integer numeric values without fraction. Example: 1, 2, 0xFFF, etc. There are different types of integer constans like:

    1. Decimal constant
    2. Hexa-decimal constant
    3. octal constant
  2. Floating-point constants

    Floating-point constants are the numeric constants having decimal points or have fractional parts. Example: 1.245, 2.55, etc.

  3. Character constants

    Character constants are those that are enclosed inside the single quotes. The character constants have the char data type. Example: ‘A’, ‘S’, etc.

  4. String Literals

    String literals are those constants that are enclosed inside the double quotes. They are the sequence of characters. String literals also have the char data type. Example: “OjhaBikash”, “Hello world”, “Constants”, etc.