Constants in C

Constants in C: It is the most fundamental and essential part of the C programming language. Constants in C are fixed values that are used in a program, and its values remain the same during the entire execution of the program.

C constants

  • These are also called literals.
  • They can be of any data types.
  • Best practice for defining constants using only upper-case names.

Syntax: const type constant_name; 

Example



#include<stdio.h> 
main() { 
const int SIDE = 10; 
int area; 
area = SIDE*SIDE; 
printf("The area of the square with side: %d is: %d sq. units", SIDE, area); 
}

Output

The area of the square with side:10 is: 100 sq. Units

Types of Constants in C

Constants are categorized into two types. They are Primary constants and Secondary constants. And again, these categories have the submodules as follows:

1) Primary Constants

Primary constants are again divided into subcategories. They are Integer, Real & Character.

Integer Constants in C

It is referring to a sequence of digits. And these Integers are of three types.
1) Decimal Integer
2) Octal Integer
3) Hexadecimal Integer 

Example

15, -256, 0, +50, 0x6

Real Constants in C

The numbers containing fractional parts like 99.25 are called real or floating-point constants.

 Character constants in C

A character constant is a single character, enclosed in single quotation marks. It means the value of a character constants can vary from one machine to next, by depending on the character set being used on a particular machine.

Example

‘x’, ‘5’, ‘;’

String Constants in C

It is a sequence of characters enclosed in double quotes and they also include letters, digits, special characters have and blank spaces.

Example

“Hello!”, “2015”, “2 + 1”

Backslash Character constants in C

C supports some character constants having a backslash in front of it. The list of backslash characters has a specific meaning which is known to the compiler. And these are also known as the “Escape sequence”. 

Example

\t is used to give the tab  
\n is used to give a new line
Constants Meaning
\a Beep sound
\b Backspace
\f form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\’ Single quote
\’’ Double quote
\\ backslash

2) Secondary Constants in C

They are having subcategories too like the primary category. And these are listed as follows:

  • Array
  • Pointers
  • Structure
  • Union
  • Enum

Regarding all these types you will be learning more once you go deep into the topics further.

const keyword

You can use the const prefix to declare constants with  specific type as follows:

Syntax: const type variable = value; 

Example

#include<stdio.h> 
int main() {
const int  LENGTH = 20; 
const int  WIDTH = 4; 
Const char NEWLINE = '\n'; 
int area;  
area = LENGTH * WIDTH;   
printf("value of area : %d", area);   
printf("%c", NEWLINE); 
return 0;}

Output
value of area: 80

Example 2

#include <stdio.h> 
void main() { 
const int height = 100;/*int constant*/ 
const float number = 3.14;/*Real constant*/ 
const char letter = 'A';/*char constant*/ 
const char letter_sequence[10] = "ABC";/*string constant*/ 
const char backslash_char = '\?';/*special char cnst*/ 
printf("value of height :%d \n", height ); 
printf("value of number : %f \n", number ); 
printf("value of letter : %c \n", letter ); 
printf("value of letter_sequence : %s \n", letter_sequence); 
printf("value of backslash_char : %c \n", backslash_char);  
}

Output
80