5 Types of Constants in C and C++ and How they’re Different from Literals

Get Certified in C Programming for Free and Take Your Skills to the Next Level

Have you ever thought, what are constants and why they are an important part of the programming world? We have got all the answers to your queries. Constants in C and C++ programming are nothing but fixed values that cannot be altered throughout the program run. These fixed values are also called as literals.

In layman language, we can use the terms constants and literals interchangeably. But, we will highlight the key difference between the two in this tutorial.

Before we start, you must be aware of the Variables in C 

Now, let us acknowledge the significance of constants and literals in C and C++ by considering the following problem:

There are certain situations where variables do not change their value, let’s say, the value of pi, approximately equal to 3.14159 is constant and it can never change which is a universal fact. We might encounter several situations, where we would require pi for mathematical calculations. Similarly, we can assign constant values to several variables according to our convenience.

1. Variables and Constants in C and C++

It is important to note that a variable connotes a different meaning in programming and mathematics. A variable is nothing but a value that we can store in computer memory. We can easily change its value during run-time. In contrast to that, constants never change their value throughout the program run. Constants can hold any of the data types available in C and C++.

2. Declare or Define Constants

We can assign C/C++ constant value to a variable in two ways:

  1. Using #define, a preprocessor directive: We have already discussed #define in detail in preprocessors.
  2. Using the keyword const: It is similar to variable declaration except that we should add the keyword “const” prior to it. It is important to assign a value to the constant as soon as we declare it.

2.1 Declare or Define Constants in C

Here is a program in C that illustrates what happens when we try to modify the value of a constant:

#include<stdio.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

const float pi = 3.14;
const float e = 2.71;

pi = 3.14159;
printf("The value of pi is: %f", pi);
return 0;
}

Output-

modify the value of a constant in C

2.1 Declare or Define Constants in C++

The same issue arises when we try to modify the value of a constant:

#include <iostream>
using namespace std;

int main()
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

const float pi = 3.14;
const float e = 2.71;

pi = 3.14159;
cout<<"The value of pi is: "<< pi <<endl;
return 0;
}

Error-

Declare or Define Constants in C++

3. Constants vs Literals in C/C++

As discussed earlier, constants and literals may be used interchangeably but there is a slight difference between the two.

Let us consider a simple example to better understand it.

In India, the legal voting age is 18.

Therefore, we define,

const voting_age = 18;

Here, 18 is a literal, a value that is expressed as itself whereas a constant can be considered a data type that is substituted in place of a literal to enhance the functionality of the code.

Now, we wish to print the message, “ You are allowed to vote! ” for people of age greater than or equal to 18.

Here is a segment of code in C in accordance with the statement above.

if (age >= voting_age)
{
printf(“ You are allowed to vote! ”);
}

Here, the identifier voting_age is constant.

Here is a segment of code in C++ in accordance with the statement above.

if (age >= voting_age)
{
cout<<“ You are allowed to vote! ”<<endl;
}

Here, the identifier voting_age is constant.

4. Types of Constants in C and C++

In the C/C++, there are 5 different types of constants depending upon their Data type:

Types of Constants in C and C++

4.1 Integer Constants

As the name itself suggests, an integer constant is an integer with a fixed value, that is, it cannot have fractional value like 10, -8, 2019.

For example,

const signed int limit = 20;

We may use different combinations of U and L suffixes to denote unsigned and long modifiers respectively, keeping in mind that its repetition does not occur.

We can further classify it into three types, namely:

  • Decimal number system constant: It has the base/radix 10. ( 0 to 9)
    For example, 55, -20, 1.
    In the decimal number system, no prefix is used.
  • Octal number system constant: It has the base/radix 8. ( 0 to 7 )
    For example, 034, 087, 011.
    In the octal number system, 0 is used as the prefix.
  • Hexadecimal number system constant: It has the base/radix 16. (0 to 9, A to F)
    In the hexadecimal number system, 0x is used as the prefix. C language gives you the provision to use either uppercase or lowercase alphabets to represent hexadecimal numbers.

4.2 Floating or Real Constants

We use a floating-point constant to represent all the real numbers on the number line, which includes all fractional values.

For instance,

const long float pi = 3.14159;

We may represent it in 2 ways:

  • Decimal form: The inclusion of the decimal point ( . ) is mandatory.
    For example, 2.0, 5.98, -7.23.
  • Exponential form: The inclusion of the signed exponent (either e or E) is mandatory.
    For example, the universal gravitational constant G = 6.67 x 10-11 is represented as 6.67e-11 or 6.67E-11.

4.3 Character Constants

Character constants are used to assign a fixed value to characters including alphabets and digits or special symbols enclosed within single quotation marks( ‘ ’ ).

Each character is associated with its specific numerical value called the ASCII (American Standard Code For Information Interchange) value.

For example, ‘+’, ‘A’, ‘d’.

4.4 String Constants

A string constant is an array of characters that has a fixed value enclosed within double quotation marks ( “ “ ).

For example, “DataFlair”, “Hello world!”

4.5 Enumeration Constants

Enumeration constants are user-defined data-types in C with a fixed value used to assign names to integral constants.

For example,

enum rainbow = { Violet, Indigo, Blue, Green, Yellow, Orange, Red }

The enumeration rainbow has integral values as:

Violet : 0
Indigo: 1
Blue: 2
Green : 3
Yellow: 4
Orange: 5
Red: 6

How to use Constant in C?

Here is a code in C that illustrates the use of some constants:

#include<stdio.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

const int value = 4;
const float marks = 98.98;
const char grade = 'A';
const char name[30] = "DataFlair";

printf("The constant int value is: %d\n",value);
printf("The constant floating-point marks is: %f\n", marks);
printf("The constant character grade is: %c\n", grade);
printf("The constant string name is: %s\n",name);
return 0;
}

Code on Screen-

Example of Constants in C

Output-

Use Constants

How to use a Constant in C++?

Here is a code in C++ that illustrates the use of some constants:

#include <iostream>
using namespace std;

int main()
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

const int value = 4;
const float marks = 98.98;
const char grade = 'A';
const char name[30] = "DataFlair";

cout<<"The constant int value is: "<< value <<endl;
cout<<"The constant floating-point marks is: "<< marks <<endl;
cout<<"The constant character grade is: "<< grade <<endl;
cout<<"The constant string name is: "<< name <<endl;

return 0;
}

Code- 

Use a Constant in C++

Output-

Output of Constant in C++

Quiz on Constants & Literals in C and C++

Summary

In this tutorial, we discussed the difference between variables and constants in C and C++. Then, we further carried on our discussion by shedding light on how to declare or define constants or literals and the types of constants available in C in detail. After completing this tutorial, you have gained command over C/C++ constants and literals.

Don’t forget to check Unions in C language

Feedbacks and Suggestions are welcomed in the comment section!

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *