Constants & Literals in C

In the last lesson, we have learned how to decare different kinds of variables based on the data type. And so far, we have learned four kinds of variable declaration using four kinds of constant values. Yes, we have declared variables using constant value. There variable was just a data type but we assigned different kinds of constant and those constants also called literals.

Understanding the Constant literals

Now we will learn the constant or literals value. As we can declare four kinds of variables using four kinds of literals then probably there are four kinds of literals support in C. They are:

1. Integer Constants/Literals

An integer constant can be decimal constant, octal constant or hexadecimal constant. And in C program, for increasing memory location, it supports the signed and unsigned bit allocation. And also have a term for the long data type, which increases to the double of the default data size.

To declare a decimal number, we directly implement the prior decimal number, and for the octal number, we use “o” as the prefix to indicate octal numbers, for the hexadecimal number we use “ox” as the prefix to indicate hexadecimal numbers. Unsigned and long numbers are declared as follows:

Example:

  • Decimal Integer: 13, 786, 349, 98, etc
  • Octal Integer: o45, o613, o44, etc
  • Hexadecimal Integer: ox54, ox897, 0x68A, etc
  • Long integer: 465l, 434l,12l, etc
  • Unsigned Integer: 374u, 56u, 879u, etc
2. Floating point or Double constants/Literals

Floating-point constant or literals are the set of all the real numbers including fractional part. For example, 45.34, 56.089, etc are floating-point constants or literals.

3. Character Constants/Literals

Every single character that is actually represented by a universal number based on ASCI code is called character constant or literals. For example ‘A’ is a character, by the rules of ASCI code the character ‘A’ is represented by the integer number 65. Like this, all the characters are represented by their corresponding number.

Example:

A, B, c, D, $, @, % are characters constants or literals

4. String Constants/Literals

When two or more single characters are written together then it is called string literals. For example, “HelloWorld”, “HelloPrograCoding, etc are called string constants.

Defining Constant

In this lesson, we will learn how to declare constants in the C programming language. We can define/declare constants using two methods. The first one is using the #define method and the second one is using the const method. We can also declare four kinds of constat like the variable using the four kinds of constant value or literals.

Note: The basic difference between constant and variable data type is, constant data can not be changed throughout the program execution, but the data of a variable can be changed as many times as you want by redeclaring the variable with other constants

1. Using the #define method

The #define is actually a preprocessor like #include that we used for including header files. But #define is used to declare or define constants in C. Once a value is declared under the #defined preprocessor then the value can not be changed throughout the program.

The syntax for declaring constants using #define:

#define constant_name constant_value

Example:

#define PI 3.1416

Rules for defining constants using #define:

  • The name of constant should be always in Uppercase letter. The lowercase letter also works. But it is a convention to use uppercase latter to distinguish the constant from the variables. Notice that we used “PI” as a constant name in the above example.
  • Always declare the constant name immediately after the header file when you use the #define preprocessor to declare a constant. it is also a convention.

Example Program:

#include <stdio.h> 
//calculating area of a circle
#define PI 3.1416

int main() {
    float radius = 5.01, area;
    area = PI * radius * radius;

    printf("Area is: %f", area);

    return 0;
}

Output:

Constant in C

By this example, you should clear your concept of defining constant in C using the #define preprocessor.

Note: Unlike variables, the value of a constant can not be changed once it is declared.

2. Using the const Keyword

Defining constants using const keyword is same as a variable declaration. But it uses a const keyword before the variable declaration.

The syntax for declaring constants using const keyword:

const data_type constant_name;

or initialized constant declaration as follows:

const data_type constant_name = constant_value;

Example program:

#include <stdio.h>

int main() {
    const float PI = 3.1416;
    float radius, area;
    
    printf("Enter radius of the circle: ");
    scanf("%f", &radius);

    area = PI * radius * radius;

    printf("Area is: %.4f", area);

    return 0;
}

If you input 2.4 as the radius of the circle then we will get the following output.
Output:

constant using const keyword in C

Test: Constant value changes or not

So far we have already mentioned several times that constant value can not be changed throughout the program execution. Let’s test it using an example program.

Example program:

#include <stdio.h> 

#define E 2.71828

int main() {
    const float PI = 3.1416;

    E = 20.43; //re-declaring value of E
    PI = 10.33; //re-declaring value of PI

    printf("Value of E: %f\n", E);
    printf("Value of PI: %f", PI);

    return 0;
}

In the above example, we tried to re-declare the values of both constants just like the variable redeclaring. If you try to run this program you will get the following error while compiling the code.

constant value can not be changed in c

In the build message, you will get the error messages that “PI and E” is read-only. That means constants value can not be changed once it is declared.

« Previous Next »