Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.
Integer(numeric) Constants
An integer constant refers to a sequence of digits. There are three types of integers, namely, a decimal integer, octal integer, hexadecimal integer.
- 11
- 31
- -71
- -24
An octal integar consists of a set of the digit,0 through 7, with a leading 0.For example:-
- 001
- 012
- 000
- 065
A sequence of digits preceded by 0x or 0X is considered as hexadecimal integar.They are alos included A to F where A represents the number 10 through 15.For example:-
- 0x2
- 0x6A or 0X6a
The rule for Integer Constants
- An integer constant must have one digit
- It must not have a decimal point
- It can be either positive or negative
- If no sign precedes an integer constant, it is assumed to be Positive
- No commas or blanks are allowed within an integer constant
- Range follow:- -2147483648 to +2147483647
Integer numbers are inadequate to represent quantities that vary continuously, such as distance, temp, price, etc…These quantities are represented by number by a number containing fractional parts like 19.087.For example:-
- 80.67
- 0.878e5
* It may be written in exponential notation.
Character Constants
A Character constant contains a single character enclosed within a pair of single quote marks. For example:-
1 |
'l' 'p' 'a' '7' '1' |
Note that the character constant ‘7’ and ‘1’ is not the same as 5 and 1.
The rule for Character Constants
- A constant is a single alphabet, a single digit, or a special symbol
- It enclosed in single quotes
String Constant
A string constant is a sequence of characters enclosed in double-quotes. The character may be letters, numbers, special characters. For examples:-
1 |
"Hello" "paul" "it" "good" |
Backslash character constants
C support some special constant that is used in output functions. For example, the symbol ‘\t’ stands for horizontal tab while ‘\v’ stands for vertical tab. A list of such backslash is given:-
1 2 3 4 5 6 7 8 9 10 11 12 |
'\a' audible alert '\b' back space '\f' form feed '\n' new line '\r' return '\t' horizontal tab '\v' vertical tag '\" single quote '\"" double quote '\?' question marks '\\' backslash '\0' NULL |
Defining Constants
- Using const keyword.
- Using #define preprocessor.
Using const keyword
You can use const word to declare constants.For example
const data_type var_name = value;
C Code Example
1 2 3 4 5 6 7 |
#include <stdio.h> void main() { const int a=7; printf("value of a is=%d" , a); } |
Using #define preprocessor
synatx
- #define identifier value
C Code Example
1 2 3 4 5 6 |
#include <stdio.h> #define C 99 void main() { printf("C %d" , C); } |