Constants in C Programming Language

Constants in C Programming Language

The C programming language offers many features and functionalities to the programmers. It has a ton of built-in functions that will help in simple and efficient coding. In C, you also use a fixed value that won’t be altered during the execution of the program.

These fixed values will help you in many ways in your programming. A programmer should learn the C programming language because it has many methods and functionalities to offer.

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

Literals in C
The main purpose of literals is to represent fixed values. These fixed values cannot be changed during the execution of the program. Also, you cannot change the values after their definition. You can use the literals directly in your program code.

For example:- 1, 3.2, ‘T’ etc.

Types of C Literals
Below are the types of literals present in C:-

Integer literals
Floating literals
Character literals
String literals

  1. Integer Literals in C:-
    There is no fractional or exponential part in integer literals. There are 3 types of integer literals present in C:-

It is used for indicating the base of the number system in which the data is stored.

0x or 0X :- hexadecimal(base-16)
0 :- Octal(base-8)
No prefix :- Decimal(base-10)
It is used for indicating the type of the value which is stored in an integer constant. You can use U(unsigned) and L(long).

Example:- Integer Literals

Decimal:- 0,6,58 etc
Hexadecimal:- 0x6f, 0x4a, 0x675 etc
Octal:- 033, 077, 015 etc
26l // long
56ul // unsigned long

  1. Floating Literals in C
    A floating point consists of 4 things:-

An Integer part
Decimal point
Fractional part
Exponential Part
A floating point literal can be either in fractional or in exponential form.

Example:- Floating Literals

NOTE:- E-5 = 105

  1. Character Literal in C
    The main purpose of a character literal is to store a single character which is enclosed within single quotes(‘’). You can also store multiple characters with the help of an array of character types.


char s = ‘T’;
Example:- Character Literal

Escape Sequences in C
There are some special types of characters like ‘\n’ and ‘\t’ present in C. These are known as escape sequences. It has special meaning in C programming. For example, ‘\n’ means newline.

Escape Sequences What it does
\b Used for backspace.
\f Used for form feed.
\n Used for a new line.
\r Used for a return.
\t Used for the horizontal tab.
\v Used for the vertical tab.
\ Used for backslash.
\’ Used for a single quotation mark.
\” Used for a double quotation mark.
\? Used for a question mark.
\0 Used for Null character.
The compiler will handle the characters differently when it executes the backslash().

  1. String Laterals in C
    You can use string laterals to store multiple characters together. It is enclosed within double quotes(“”).


char s[]=”TechVidvan”;


int main() {
char s[] = “TechVidvan Tutorial: String Lateral!”;
printf(“%s”, s);
return 0;
TechVidvan Tutorial: String Lateral!

Constants in C
In C, you can use const keyword to define a variable as constant. After defining it as constant, you cannot change its value.

Defining constants in C:-
In C, there are two ways to define a constant such as:-

By Using const keyword
By Using #define preprocessor directive

  1. Using const keyword in C
    You can use the const keyword to define a constant variable in C.


const dataType variable_name = value;
Example:- Using const keyword


int main() {
const int val = 4;
val = 5; // changing the value 4 to 5!
printf(“%d”, val);
return 0;

main.c: In function ‘main’:
main.c:13:7: error: assignment of read-only variable ‘val’
val = 5; // changing the value 4 to 5!
From the above output, you can clearly understand that you cannot change the value of a constant variable.

  1. Using #define preprocessor directive in C
    It is used for creating and defining macros in C. With the help of macros, you can declare a constant value which you can use throughout the program.


define name_of_the_constant value

Example:- #define directive


define SIDE 7

int main() {
int area;
area = SIDE*SIDE;
printf(“TechVidvan Tutorial: #define directive!\n\n”);
printf(“Area of square: %d”, area);
return 0;
TechVidvan Tutorial: #define directive!

Area of square: 49

In this tutorial, we learnt about constants and literals in C. We also discussed the types of literals. We discussed 2 ways to define constants in C. Constants and Literals help programmers in simple and easy coding.

Leave a comment