BEST TUTORIAL FOR PROGRAMMING        
BEST TUTORIAL FOR PROGRAMMING

Constants In C

Constant definition : Constant means that a fixed value that cannot be changed or altered and in programming, a constant is a value that cannot be changed by the program during its normal execution. Constants are fundamental and essential part of any programming language. Constants are also called literals.


C Constants

: Constants in C programming are the fixed values which cannot be changed or modified by the C program during its normal execution once they are defined. Constants are same as normal variables, but only difference is that their values cannot be changed by the program once they are defined. Constants can be any of the data types like integer, float, character, etc.




What is the need of constant variables in programming?

Suppose, we have created a program that evaluates 10 mathematical problems using PI (3.14159). We run the program and value of the PI is changed by the program while execution. In this case, we will not get result as expected. To solve this problem, we can define PI value as a constant value and as we know that constant value cannot be changed by the program once it is defined




Some key points about C constants are given below :

1 
Constants are also called literals
2 
Constants can be any of the data types
3 
It is considered best practice to define constants in uppercase
4 
Constants are same as normal variables, but only difference is that their values cannot be changed by the program once they are defined




Define constants in C

There are two way to define constants or constant variables In C which are given below.


1 

Using C const keyword.

2 

Using C #define preprocessor directive




C const keyword

Defining constants using C const keyword is simple and similar to defining normal variables, only we need to write 'const' just before data type of a variable's definition. A program is given that shows how to use C const keyword to define constants of different data types in C programming.


Syntax of C const keyword

const < Data Type > < Constant Name > = < Constant value >;


Example of C const keyword to define constants in C

#include <stdio.h>
#include <conio.h>

int main() {

const int INT_CONST = 10; // int constant
const float FLOAT_CONST = 20.44; // float constant
const char CHAR_CONST = 'V'; // char constant
const char STRING_CONST[50] = "vktutorial"; // string constant

printf("Integer constant : %d \n", INT_CONST);
printf("Floating point constant : %f \n", FLOAT_CONST);
printf("Character constant : %c \n", CHAR_CONST);
printf("String constant : %s \n", STRING_CONST);

getch();
return 0;
}

Output :

Integer constant : 10
Floating point constant : 20.440001
Character constant : V
String constant : vktutorial





C #define preprocessor directive

C #define preprocessor directive is used to give alias name to the existing variable or value. We can also use this preprocessor directive to declare or define constants in C programming. A program is given below that shows how to use this preprocessor directive to declare or define constants in C programming.


Syntax of C #define preprocessor directive to declare a constant

#define < Constant Name > = < Constant value >;


Example of C #define preprocessor directive to declare constants in C

#include <stdio.h>
#include <conio.h>

#define INT_CONST 10 // integer constant
#define FLOAT_CONST 20.44 // floating-point constant
#define CHAR_CONST 'V' // char constant
#define STRING_CONST "vktutorial" // string constant

int main() {

printf("Integer constant : %d \n", INT_CONST);
printf("Floating point constant : %f \n", FLOAT_CONST);
printf("Character constant : %c \n", CHAR_CONST);
printf("String constant : %s \n", STRING_CONST);

getch();
return 0;
}

Output :

Integer constant : 10
Floating point constant : 20.440001
Character constant : V
String constant : vktutorial





Try to change C constant value

If we try to change a constant value in a C program once it is defined, then the program will not run and give an error at compile time. A C program is given below to make this thing more clear.


C program
#include <stdio.h>
#include <conio.h>

int main() {

const float PI = 3.14159; // PI is a constant variable.

PI = 3.00000; // Trying to change PI constant value

printf("PI Value : %f", PI);

getch();
return 0;
}

Output :

error : assignment of read-only variable 'PI'



Literals in C

C literals : Constants or fixed values are also called literals. A variable is a name that can represent different values during the execution of the program and a constant is a name that represents the same value throughout a program. But a literal is a value that is expressed as itself like 14, 10.54, 'v', "vk", etc.




Types of C constant

1 

Integer constants

2 

Floating point constants

3 

Character constants

4 

String constants

5 

Escape Sequence constants




Integer constants in C

An integer constant is a whole number or numeric constant (1412) without any fractional or exponential part. It can either be positive or negative. The range of integer constants is -32768 to 32767 but it can be variant according to architecture of systems or platforms. There are three types of integer constants in C programming which are given below.

  1. Decimal integer constant (base 10)
  2. Octal integer constant (base 8)
  3. Hexadecimal integer constant (base 16)



C decimal integer constant

A decimal integer constant can have any number of digits from '0' to '9'. It can not contain any other character except '+' and '-'. If it has '+' and '-', then they must be used before the number like +14, -12, etc. Its base is 10.

For example : 0, +19, -93, etc.



The following table shows some invalid decimal integer constants.

Decimal Constant/Literal Validity Reasons
10,00 Invalid Comma (,) is not allowed
0.5 Invalid Decimal point (.) is not allowed
14 12 Invalid Space is not allowed
-32769 Invalid Out of range for 16 bit machine
32768 Invalid Out of range for 16 bit machine
50000 Invalid Out of range for 16 bit machine



C octal integer constant

An octal integer constant can be any number made from the combination of digits from '0' to '7'. The octal integer constant starts with a zero (0) and it can not contain any other character except '+' and '-'. If it has '+' and '-', then they must be used before the number like +014, -012, etc. Its base is 8.

For example : 010, 0777, -033, etc.



The following table shows some invalid octal integer constants.

Octal Constant/Literal Validity Reasons
10,00 Invalid Comma (,) is not allowed
0.5 Invalid Decimal point (.) is not allowed
14 12 Invalid Space is not allowed
085 Invalid 8 in octal is invalid
777 Invalid Octal integer must be started with a zero (0)



C hexadecimal integer constant

There can be any combination of digits from '0' to '9' and characters from 'A' to 'F' or 'a' to 'f' in hexadecimal integer constant. The hexadecimal integer constant starts with a 0x or 0X and its base is 16.

For example : 0x7f, 0XAB, 0XA122, etc.



The following table shows some invalid hexadecimal integer constants.

Hexadecimal Constant/Literal Validity Reasons
10,000 Invalid Comma (,) is not allowed
0x122.44 Invalid Decimal point (.) is not allowed
0x14 12 Invalid Space is not allowed
0xABG Invalid G in hexadecimal is invalid
012345 Invalid Hexadecimal integer must be started with a 0x or 0X


INFO : An integer constant or literal can also have U and L as suffix. U or u for unsigned and L or l for long. for example : 14L, 12u, 1993l, etc. If no sign precedes an integer constant, it is assumed to be positive.




Floating point constants in C

A floating point constant is a numeric constant that has either a fractional form (decimal point) or an exponent form. It can either be positive (+) or negative (-). The floating point constant is also called real constant.

For example (fractional form) : 1.0, -0.5, -.4, +.93, +86., etc.

For example (exponent form) : 50e2, -0.545E4, -0.24E-5, 314159E-5L, etc.



The following table shows some invalid floating point or real constants.

Floating Point Or Real Constant/Literal Validity Reasons
124.0/5 Invalid Forward slash (/) is not allowed
129 Invalid Decimal point (.) is missing
2.4E 12 Invalid Space is not allowed
2.4.6 Invalid No more than one decimal point is allowed
14, 12.34 Invalid Comma (,) is not allowed
10.5e-6.5 Invalid Fractional form 6.5 is not valid in the exponent part




Character constants in C

A character constant contains a single character enclosed within single quotes (a pair of single quote). The single character can be a single alphabet, a single digit, a single special symbol or a single escape sequence character. A unique value is attached with each character that is known as ASCII value (American Standard Code for Information Interchange).

For example : 'v', 'x', '4', '$', etc.



The following table shows some valid and invalid character constants.

Character Constant/Literal Validity Reasons
'x' Valid -
'0' Valid -
'#' Valid -
'\t' Valid Escape sequence character
'\n' Valid Escape sequence character
"k" Invalid Two double quotes ("") are not allowed
'vk' Invalid Two characters are not allowed




String constants in C

A string constant contains sequence of characters enclosed within double quotes (a pair of double quotes). It can include alphabets, digits, special characters, blank spaces and escape sequence characters.

For example : "v", "vk tutorial", "1412", "$1993", etc.



The following table shows some invalid string constants.

String Constant/Literal Validity Reasons
'x' Invalid Should be enclosed between double quotes (" ")
'tutorial' Invalid Should be enclosed between double quotes (" ")




Escape sequence constants in C

Escape sequence character starts with backslash '\' and after this backslash there comes one or more characters. Backslash with some characters changes the meaning of those characters and generates special printing effects. Since escape sequence characters begin with backslash '\', thus these are also called backslash constants.

For example : \n, \t, \\, etc.



The following table shows all important backslash constants or escape sequence characters of C.

Escape Sequence/Backslash Character Character Meaning/Purpose
\a Bell Beep sound
\b Backspace Cursor moves towards left by one position
\f Form Feed Cursor moves to the next page
\n New Line Cursor moves to the next line
\r Carriage Return Cursor moves to the start of current line
\t Horizontal Tab Cursor moves towards right by 8 positions
\v Vertical Tab This is usually not supported
\' Single Quote It is used to print '
\" Double Quotes It is used to print "
\\ Backslash It is used to print \
\? Question Mark It is used to print ?
\0 NULL NULL character


INFO : In next section, we will learn about escape sequence or backslash characters in detail.