The constants are fixed values which cannot be altered during the execution of a program. It provides a way to define a variable which cannot be modified by any other part in the code.
Constants can be defined by placing the keyword const
in front of any variable declaration. If the keyword volatile
is placed after const, then this allows external routines to modify the variable (such as hardware devices). This also forces the compiler to retrieve the value of the variable each time it is referenced rather than possibly optimizing it in a register.
const type constant name;
There are various types of constants in C.
These constants refer to a sequence of digits. Any representation can be used to specify an integer constant. Below table shows ways to specify an integer constant, whose value is 1234 in decimal number system, in different number systems.
Representation | Value |
---|---|
Decimal | 1234 |
Hexadecimal | 0x4D2 |
Octal | 02322 |
These constants refer to those numbers which contain fractional parts like 12.34. Such numbers are called real or floating point numbers. A real constant can also be expressed in exponential or scientific notation. As an example, 12.34 can also be expressed as 1234E-2. In this form, the real number is expressed in terms of mantissa and exponent. The mantissa is either a real number or an integer and exponent is an integer number. Both can have an optional plus or minus sign. The letter E separating the mantissa and the exponent can be written in lowercase also.
These constants refer to those tokens which contain a single character enclosed within a pair of single quotes like ‘W’. The character may be a letter, number, special character or blank space. As an example, ‘5’ is a character constant while as 5 is an integer constant. Similarly, 5.0 can be treated as real constant.
Character constants are printable characters which are stored in memory as their ASCII (American Standard Code for Information Interchange) code.
These constants refer to those tokens which contain a string of characters enclosed within a pair of double quotes. The characters may be letters, numbers, special characters and blank space. Examples include “Hello World!”, “123”, and so on. It should be noted “123” is a string constant while 123 is integer constant.
These constants refer to those special character constants which contain a backslash and some character enclosed within a pair of single quotes. These constants are used in output functions and are known as escape sequences. Below shows the list of backslash character constants along with their interpretation.
Backslash Character | Meaning |
---|---|
\a | alert (bell) character |
\b | backspace |
\f | form feed |
\n | new line |
\r | carriage return |
\t | horizontal tab |
\v | vertical vab |
\\ | backslash |
\? | question mark |
\' | single quote |
\" | double quote |
Below are the examples of various constants.
Constant | Example |
---|---|
Integer Constant | 10, 20, 30, 450 etc. |
Real or Floating-point Constant | 10.3, 20.2, 450.6 etc. |
Octal Constant | 021, 033, 046 etc. |
Hexadecimal Constant | 0x2a, 0x7b, 0xaa etc. |
Character Constant | 'a', 'b', 'x' etc. |
String Constant | "c", "c program", "c in rookienerd" etc. |
Backslash character constants | \a, \n etc. |
There are two ways to define constant in C programming.
The const
keyword is used to define constant in C programming.
const float PI=3.14;
Now, the value of PI variable can't be changed.
#includeint main(){ const float PI=3.14; printf("The value of PI is: %f",PI); return 0; }
If you try to change the the value of PI, it will render compile time error.
#includeint main(){ const float PI=3.14; PI=4.5; printf("The value of PI is: %f",PI); return 0; }
The #define
preprocessor is also used to define constant. We will learn about #define
preprocessor directive later.
Visit here for: #define preprocessor directive.