C Programming

Constant in C Language

Any language consists several letters called alphabet. These alphabets are called identifiers in the C language.

Identifiers have three types:

  1. Constant
  2. Variable
  3. Keyword

Let’s discuss about Constant. In general, anything that is not changed is constant. But in C language, a software always manages data or information. This information is called Constant.

Data = Information = Constant

Const is a keyword in C language. It is identified as a Qualifier. A Qualifier Const can be applied to the declaration of any variable to specify that its value will not be changed.

Classification of Constant

There are two types of Constant present in the C language. They are:

  1. Primary Constant
  2. Secondary Constant

1. Primary Constant

Primary Constant has three types:

  • Integer

Example: -55 , 26, 0 etc.

  • Real

Example: 19.6, -1.65, 3.1 etc.

  • Character

Example: ‘ c ’, ‘ j ’, ‘ + ’, ‘ 3 ’ etc.

2. Secondary Constant

They are various types of Secondary Constant:

  1. Array
  2. String
  3. Pointer
  4. Union
  5. Structure
  6. Enumerators

Historical Fact:

Const was not applicable in the early use of the C language; the concept was borrowed from C++.

Usage:

The keyword Const can be applied to any declaration including structure, unions, and enumerated types or typedef names. The process of applying the keyword Const to a declaration is called “qualifying the declaration”. Const means something is not modifiable.

Programming Example 1:

#include <stdio.h>

int main ()
{
const int x= 5;
    x++;
printf("x = %d",x);
return 0 ;
}

Output:

Explanation:

The program produced a compiler error because we tried to increment the value of x. X is constant and we cannot change the values of a constant.

Programming Example 2:

#include <stdio.h>

int main ()
{
    const int x;
printf("x= %d ",x);
return 0 ;
}

Output:

Explanation:

In this example, we declared a constant variable “x” without initialization. If we do not initialize at the time of declaration for a constant, the garbage value will be assigned to x and it will not be initialized. For this const variable, we have to initialize at the time of declaration. In this particular program, the garbage value of constant x is zero.

Programming Example 3:

#include <stdio.h>

int main ()
{
    const int x=5;
int *p;
    p = &x;
printf(" x = %d",x);
    ++ (*p);
printf(" x = %d",x);
    return 0 ;
}

Output:

Explanation:

In this program, we tried to change the value of a constant with the help of a pointer. Pointer refers to the address of a constant.

We can modify the value of any const variable when we declare a pointer “int *p”. P is a pointer which points to the int type of a variable.

If we modify through the variable “x” by declaring a pointer “++( *p )-> *p”, the result is a block. But if we modify through the variable “p”, an error will occur.

Constant values remain unchanged throughout the program, but we can modify the value of a constant through a pointer.

Programming Example 4:

#include <stdio.h>

int main ()
{
    const int x =5;
    const int *p;        //pointer to const
    p= &x;
printf("x = %d",x);
    ++ (*p);
printf("x= %d",x);
return 0 ;
}

Output:

Explanation:

In this programming example, we used another Const pointer. By using a Const pointer, we cannot change the value of a pointer. It means that we can’t declare a “p++” pointer since it is not a Const and the variable it points is constant. Therefore, a pointer “++(*p)” is not applicable. To declare a Const pointer to Const, the pointer must be a Const and the variable it points is also a Const.

How to declare a pointer:

const int *p;

int const *p;

Both are the pointers to constant.

How to declare a const pointer:

int *const p;

Programming Example 5:

#include <stdio.h>

int main ()
{
    const int x= 5;
    int *const p;
    p= &x;
printf(" x= %d", x);
    ++ (*p);
printf(" x= %d", x);
return 0 ;
}

Output:

Explanation:

In this programming example, the value of x is a Const. The pointer itself is a Const. Changing the value of a pointer is not possible; an error will occur.

The declared variable is “int *const p= &x” where “p” is initialized at the time of declaration.

The pointer itself is a Const but the pointer to Const is not applicable.

Programming Example 6:

#include <stdio.h>

int main ()
{
    const int x=5;
    int *const p= &x;
printf("x= %d",x);
    ++p;
printf("x= %d",x);
return 0 ;
}

Output:

Explanation:

This particular program cannot be compiled. Since it is a pointer Const, we cannot change the value of “p”.

Programming Example 7:

#include <stdio.h>

int main ()
{
    const int x=5;
    const int *const p= &x;
printf("x= %d",x);
    ++(*p);
printf("x= %d",x);
return 0 ;
}

Output:

Explanation:

A compile time error occurred in this particular program because we cannot change the value of “p” since it is a Const pointer to Const.

Conclusion

Constant is a powerful basic concept of the C language. There are many types of constant present in the C language. Without the concept of constant, data or information cannot be managed properly by the software in the C language. Related articles are available on the website.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com