Constants In C

Constants In C

In the world of programming, constants play a foundational role. They are like unchanging pillars of information, providing stability and clarity to the code. In C, one of the most widely used programming languages, constants are vital elements that maintain their values throughout the program’s execution. These unchanging values are not only crucial for maintaining code integrity but also for enhancing readability and maintainability. In this exploration of constants in C, we will look into their types, usage, and the fundamental role they play in shaping the behavior of C programs.

What is Constant in C

In C programming, a constant is a variable that remains unchanged once it is declared in the program. Once you define a constant, you cannot alter its value during the program’s execution. Constants are essentially values that are fixed and do not vary throughout the program.

How to Define Constant in C

In the C language, we use the const keyword to define a constant. This keyword, often referred to as a “const type qualifier,” is positioned at the beginning of a variable declaration to indicate that the variable is a constant. Once declared as a constant using const, the variable’s value cannot be changed throughout the program.

Example of Constants in C

// C program to illustrate constant variable definition
#include <stdio.h>
 
int main()
{
 
    // defining integer constant using const keyword
    const int int_const = 25;
 
    // defining character constant using const keyword
    const char char_const = 'A';
 
    // defining float constant using const keyword
    const float float_const = 15.66;
 
    printf("Printing value of Integer Constant: %d\n",
           int_const);
    printf("Printing value of Character Constant: %c\n",
           char_const);
    printf("Printing value of Float Constant: %f",
           float_const);
 
    return 0;
}

Output

Printing value of Integer Constant: 25
Printing value of Character Constant: A
Printing value of Float Constant: 15.660000

Note: In C, when we create a constant variable, we have to give it a value right when we create it. If we forget to do that, it might end up with some random, useless value, and we won’t be able to change it later

Types Of Constants in C

The type of the constant is the same as the data type of the variables. The types of constants are given below

  • Integer Constant
  • Character Constant
  • Floating Point Constant
  • Double Precision Floating Point Constant
  • Array Constant
  • Structure Constant

Properties of Constant in C

The important properties of constant variables in C defined using the const keyword are as follows:

1. Initialization with Declaration

In C, constant variables must be initialized at the time of their declaration. If you don’t provide an initial value when declaring a constant variable, it will typically contain an undefined or “garbage” value. Therefore, it’s essential to assign a value to a constant variable when declaring it to ensure it holds a meaningful and specified value throughout its lifetime in the program.

2. Immutability

In C, constant variables are unchangeable once they are defined. This means you can only set their initial value once in the entire program, and after that, you cannot modify the value stored in that variable. Once a constant variable is set, it remains the same throughout the program’s execution.

// C Program to demonstrate the behaviour of constant
// variable
#include <stdio.h>
 
int main()
{
    // declaring a constant variable
    const int var;
    // initializing constant variable var after declaration
    var = 20;
 
    printf("Value of var: %d", var);
    return 0;
}

Output

In function 'main':
10:9: error: assignment of read-only variable 'var'
10 |     var = 20;
   |         ^


Key Difference between Constants And Literals

The constant and literals are often confused as the same. But in C language, they are different entities and have different semantics. The following table list the differences between the constants and literals in C:

ConstantLiterals
Constants are variables that can’t be modified once it is declared.
Literals are fixed-values who define themselves.
Literals are fixed-values who define themselves. Literals are indeed the actual values that are assigned to variables or used in expressions. They are fixed, concrete values, such as numbers or characters, that are directly written into the code.
The address of the constant can be found.
It cannot determine the address of a literal, except in the case of string literals.
They have lvaluesThey have rvalues
They have valuesAn example for rvalues is 24,15.5, ‘a’, “Geeks”, etc.

Defining Constant using #define Preprocessor

You can define constants in C using the #define preprocessor directive. These constants, often referred to as macros, behave like constants.

Syntax of Constant in C using #define

#define const_name value

Example :

// C Program to define a constant using #define
#include <stdio.h>
#define pi 3.14
 
int main()
{
 
    printf("The value of pi: %.2f", pi);
    return 0;
}

Output

The value of pi: 3.14

FAQ- Constants In C

Q1. What is constant and its type?

Ans. In C, constants are categorized by their values, and their types are determined by those values. Common types include integers (0 to 2147483647), strings, hexadecimal numbers, and bit values. No explicit type declarations are needed; C infers the types based on context.

Q2. What is static and constant?

Ans. In C, “static” is a storage specifier, while “const” or “constant” is a type qualifier. “Static” can be assigned for reference types and set at runtime, while constants are set at compile-time and assigned for value types only.

Q3. What are the two types of constants in C?

Ans. In C, constants are categorized into two main types: primary and secondary. These primary and secondary constants further have subcategories. You can declare constants in C using two methods: by using the const keyword or by using the #define preprocessor directive.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment