Constants are used to specify fixed values in C programming. C constants are also like general variables, the only difference is that their value cannot change or modified by programs once they get initialized.
- If you try to change the value, the program will give a compile time error.
- We can use constants in the various scenarios when we want to use the same value entire whole program.
- const keyword is used to declare constants in C programming.
- Constants are also called as literals.
- Constants are faster than variables in execution.
Syntax Declaration
Example
Types of Constants in C
There are two types of constants in C programming language.
- Numeric constants
- Character constants
1. Numeric constants
An numeric constant refers to a sequence of number digits for example 132, -532, 101.001 etc. there are 4 types of numeric listed below.
- Decimal integer
- Octal integer
- Hexadecimal integer
- Floating Point or Real Constant
a. Decimal Integers
Decimal integers can contains a set of number digits, 0 through 9 are called as decimal integers. Decimal integers can preceded by an optional + or – sign, for example 321, -545, 0, 345, +8462 etc.
Note: Decimal integer does not allows embedded space, comma and non-digit characters between number digits for example 23 421, 100,000, $13400 etc.
b. Octal Integers
An Octal Integer constant consist of any combination of digits from the set of 0 through 7, with a leading 0. Let’s take some example of octal integer constant.
0224, 0, 0432, 0421, etc.
c. Hexadecimal integer
A sequence of digits preceded 0x or 0X is considered as Hexadecimal integer. They can also include alphabets a,b,c,d,e,f. The alphabet a denotes number 10, similarly b,c,d,e,f denotes the numbers 11,12,13,14,15 respectively. Below we have listed some example of hexadecimal integer examples.
0X9a2, 0xf2a, 0xac53, 0x, 0Xacd, etc.
d. Floating Point or Real Constant
A Floating point constant refers a sequence of number digits including a fractional point. Floating point constant are also called as real constant, because it’s represent real numbers. Below we have listed some floating point constant examples.
3.531, +.7, -32.561, .521, 0.0012, etc.
2. Character constants
Character constants further categorized into two categories
- Single character constants
- String constants
a. Single character constants
A Character constant refers a single character in coated within single quotation. For example ‘a’, ‘b’, ‘m’, ‘d’ etc.
b. String constants
A String constant refers a group of character in coated within double quotation. For example "my name is sam", "hello", "today is friday" etc.
Some other useful constants
a. Escape Sequences
In C language escape sequences are represented by \ followed by character or symbol. Escape sequences have specials meanings and they can’t use in general writing. We write escape sequences at required places only where we want the meaning of that escape sequence happen there. Let’s see the list of all escape sequences available in C language in table.
Escape Sequenses |
Meanings |
\b |
Backspace |
\f |
Form feed |
\n |
New line |
\r |
Carriage return |
\t |
Horizontal tab |
\" |
Double quote |
\' |
Single quote |
\\ |
Backslash |
\v |
Vertical tab |
\a |
Alert or bell |
\? |
Question mark |
\N |
Octal constant (N is an octal constant) |
\XN |
Hexadecimal constant (N – hex.dcml cnst) |
\0 |
Null character |
b. Enumeration constants
You can also define enumeration constant by using enum keyword, see below exaple
The const Keyword
The keyword const uses as prefix of any data type to declare a constant. Let’s see how we can use const keyword to declare a constant with the help of a program.
Output
Constant Defining using #define Preprocessor
You can also define constant using #define preprocessor, Let's understand it by taking a program exaple.
Output