Constant literals in C Language [ With Examples ]

Hello Friends in this article, we learn in detail about What is Constant in C Language and what is literals in C Language, and how they are used in C Language.

First, we will learn about Constants, then about literals, because without knowing about Constants, you will not understand literals very well, 

So let’s learn What is Constant in C Language

Constant literals in C Language [ With Examples ]
Constant literals in C Language [ With Examples ]

Constant in C Language

All of you have read about Constant and Variable in Maths. In Maths the meaning of constant is, we cannot change it And the meaning of variable is, we can change its value. 

In the same way, in C Language, we read two such Variables, a Normal Variable, and a Constant Variable.

Normal variables are variables whose value we can change many times during the execution of the program and the constant variable is a variable which after declaring once, does not change during the execution of the entire program.

In simple words, Constants are a value or variable that once declare, there are no changes till the execution of the entire program. Constants are fixed values.

Constants are just like a normal variable, but there is only a little difference in it that once the value in the constant variable is declared, then after that it can not be changed during the execution of program

Let’s see how we can use Constants in C language.

How to Use Constants in C Language

In C language we can use Constants in two ways -:

  • By const keyword
  • By #define Preprocessor Directive

1. By Const Keyword

Const is a Keyword using which, we declare a Constants type variable, and once the declaration of the value of such variable then cannot be changed during the execution of entire program.

Syntax -:

const data_type variable_name;

Example -:

const float PI = 3.14;

We can use const keyword with any data type and when we create a variable using the const keyword then, the value that is assigned at the time of declaration of that variable remains, the same until the entire program runs. It can never be changed.

If you try later to change the value of such variable, then the compiler gives an error.

Come, we understand these things with this one example so that these things will be clear to you.

Example 1 -: In this example, we will not change the value of Constant variable and see what the output is.

#include <stdio.h>
int main ()
{
 const float PI = 3.14;
 printf("The value of PI is: %f", PI);
 return 0;
}

Output -:

The value of PI is: 3.14

Example 2 -: In this example 2 we will change the value of constant variable and see what the output is.

#include <stdio.h>
int main ()
{
const float PI = 3.14;
PI = 40.5;
printf ("The value of PI is: %f", PI);
return 0;
}

Output -:

Compile Time Error: assignment of read-only variable 'PI'

We saw two examples above, in the first example, we didn’t change the value of constant variable so that the output was correct, and in Example 2 we tried to change the value of constant variable whose result was that our program did not run and the error comes up with “assignment of read-only variable ‘PI'”. 

Which means we can’t change the value of Constant variable after it’s declared.

Let’s now look at another way of using Constants.

2. By #define Preprocessor Directive

In C language, we can also declare constants by #define. We will read about #define in the Preprocessor Directive section. For now, let’s see just one example of this.

Example -:

#include <stdio.h>
#define PI 3.14 // Declaration of Constant
int main ()
{
 int r;
 float s;
 printf ("Enter the radius of circle:");
 scanf ("%d", &r);
 s = r*r*PI;
 printf ("The radius of circle is: %f", s);
 return 0;
}

Output -:

Enter the radius of the circle: 5
The radius of the circle is: 78.500000

There are 6 types of Constants in C language -:

Types of Constants in C

  1. Integer constants
  2. Real or Floating-point constants
  3. Octal & Hexadecimal Constants
  4. Character Constants
  5. String constants
  6. Backslash character constants
Constant typedata type (Example)
Integer constantsint (43, 862, -678 etc )unsigned int (5000u, 1000U etc)long int, long long int (433,247 2,197,483,688)
Real or Floating point constantsfloat (15.456789)doule (650.123456789)
Octal constantint (Example: 013 /*starts with 0 */)
Hexadecimal constantint (Example: 0x90 /*starts with 0x*/)
Character constantschar (Example: ‘X’, ‘Y’, ‘Z’)
String constantschar (Example: “CsTutorialpoint”, “Hai”)

Till this time we have seen, what are the constants in C language, what are the types of constants and how can one use Constants in C language?

Let’s now understand what is literals in C language and what is their use in C language.

What are literals in C language?

When we create a program in C language and declare a variable using the const keyword, then such variable are called Constant variable, and the value assigned to such constant variable is called Literals.

For Example -: const int x = 25; In this, variable x is a constant type and the 25 is value an integer type literals.

Types of Literals

Literals in C language are of 4 types –

  • Integer literal
  • Float literal
  • Character literal
  • String literal

List of Constants Literals in C With Example

Constant Literals in CExamples
Integer literalDecimal: 0, -9, 22 etc.Octal: 021, 077, 033 etc.Hexadecimal: 0x7f, 0x2a, 0x521 etc.
Floating-point Literals-2.0, 0.0000234, -0.22E-5, 2.45
Character literal‘x’, ‘y’, ‘z’, ‘a’, ‘b’, ‘c’
String literal“Welcome”, “to”, “CsTutorialpoint”,
List of Constants Literals in C

Conclusion -:

Friends, I hope you have found the answer to your question and you will not have to search about the constant and literals in C language 

However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts 

Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the constant and literals in C language.

To get the Programming Language, Coding, C, C ++, related information subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.

Leave a Comment