There are many different types of data values that are implicitly declared as constants in C. The value of a constant cannot be changed during execution of the program, neither by the programmer nor by the computer. The character ‘A’ is a constant having numerical value equal to 65 in decimal number system.
Similarly ‘B’, ‘C’, etc., are other constant values, for instance, ‘B’= 66, ‘C’ = 67, etc. In any C program, the value of character ‘A’ cannot be changed. Similarly, a sequence of characters enclosed between double quotes such as “Morning” is a string constant. Also, the characters “\n” and “\t’ are constants, which have special meaning for compiler, and are known as escape sequences.
The digits or sequence of digits are also constants. These may be decimal digits (base 10), octal digits (base 8), or hexadecimal digits (base 16). Sequences of digits of base 10 are called decimal constants. Similarly, digital sequences of base 8 are called octal constants and sequences of hexadecimal digits are called hexadecimal constants. Different codes are used for their identification. The octal numbers are preceded by zero (0), and the hexadecimal numbers are preceded by 0x or 0X. Therefore, a decimal sequence of digits should not start with 0, because, in that case, it would be taken as octal number by the computer. C supports several types of constants.
We’ll be covering the following topics in this tutorial:
Integer Constants
An integer constant is a sequence of digits from 0 to 9 without decimal points or fractional part or any other symbols. There are 3 types of integers namely decimal integer, octal integers and hexadecimal integer.
Decimal Integers consists of a set of digits 0 to 9 preceded by an optional + or – sign. Spaces, commas and non digit characters are not permitted between digits. Example for valid decimal integer constants are
int y = 123; //here 123 is a decimal integer constant
Octal Integers constant consists of any combination of digits from 0 through 7 with a O at the beginning. Some examples of octal integers are
int x = 0123; // here 0123 is a octal integer constant .
Hexadecimal integer constant is preceded by OX or Ox, they may contain alphabets from A to F or a to f. The alphabets A to F refers to 10 to 15 in decimal digits. Example of valid hexadecimal integers are
int x = 0x12 // here Ox12 is a Hexa-Decimal integer constant
Real Constants
Real Constants consists of a fractional part in their representation. Integer constants are inadequate to represent quantities that vary continuously. These quantities are represented by numbers containing fractional parts like 26.082. Example of real constants are
float x = 6.3; //here 6.3 is a double constant. float y = 6.3f; //here 6.3f is a float constant. float z = 6.3 e + 2; //here 6.3 e + 2 is a exponential constant. float s = 6.3L ; //here 6.3L is a long double constant
Real Numbers can also be represented by exponential notation. The general form for exponential notation is mantissa exponent. The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign.
Single Character Constants
A Single Character constant represent a single character which is enclosed in a pair of quotation symbols.
Example for character constants are
char p ='ok' ; // p will hold the value 'O' and k will be omitted char y ='u'; // y will hold the value 'u' char k ='34' ; // k will hold the value '3, and '4' will be omitted char e =' '; // e will hold the value ' ' , a blank space chars ='\45'; // swill hold the value ' ' , a blank space
All character constants have an equivalent integer value which are called ASCII Values.
String Constants
A string constant is a set of characters enclosed in double quotation marks. The characters in a string constant sequence may be a alphabet, number, special character and blank space. Example of string constants are
"VISHAL" "1234" "God Bless""!.....?"
Backslash Character Constants [Escape Sequences]
Backslash character constants are special characters used in output functions. Although they contain two characters they represent only one character. Given below is the table of escape sequence and their meanings.
Constant | Meaning |
‘\a’ | .Audible Alert (Bell) |
‘\b’ | .Backspace |
‘\f’ | .Formfeed |
‘\n’ | .New Line |
‘\r’ | .Carriage Return |
‘\t’ | .Horizontal tab |
‘\v’ | .Vertical Tab |
‘\” | .Single Quote |
‘\”‘ | .Double Quote |
‘\?’ | .Question Mark |
‘\\’ | .Back Slash |
‘\0’ | .Null |