What is a Constant?
In C programming language and other languages, constants are variables with a exception that the value of a constant can be set only once and can not be modified later. This exception differs a constant variable from other variables and are called constant because their values are not variable. Value of a constant can not vary during the execution of a program, thus it can not be called a variable.
Using constants for unchangeable values ensure that the value remains the same over the period of execution, will not change mistakenly.
In programs which embeds third party codes or scripts, constants make sure that the value will not be changed by third party codes intentionally or mistakenly.
In C programming language, there are a few types of constants. The different types of constants are as follows
- Integer constant
- Real constant
- Single Character constant
- String constant
An integer constant can hold an integer as its value. There are three types of integer constants, namely, decimal integer, octal integer and hexadecimal integer.
Examples, 123 -321 0 64932
Octal integer consists of a set of digits from 0 to 7, with a leading 0.
Examples, 037 0 0437 0551
Hexadecimal integers are a sequence of digits preceded by 0x or 0X. They may also includes letters from A to F or from a to f. The letters represents the numbers from 10 to 15.Examples, 0X2 0x9F 0Xbcd 0x
Real constants can be assigned real values, that is values with floating points. Real constants are used to represent quantities that are very continuous, such as distances, temperature etc. These quantities are represented by numbers containing fractional parts. Examples, 0.00832 -0.75 33.337
Single character constants:
A single character constants contains a single character enclosed within a pair of single quote marks. Example, ‘5’ ‘X’ ‘;’
A string constant contains a string of characters enclosed within a pair of double quote marks. Examples, “Hello !” “1987” “?….!”
Backslash character constants:
C supports some special backslash character constants that are used in output functions. For example