A constant is a value that does not change during execution of the program. A "constant" is a number, character or character string that can be used in a program as a value.
C supports several types of constants
- Integer constants
- Real or Floating Point (Float) constants
- Character constants
An integer constant can be decimal, hexadecimal or octal. Prefix specifies the base.
For Example :
OX1A /*Hexadecimal */
ox1a /*Hexadecimal */
An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.
12l /*long int*/
12L /*long int*/
12u /*unsigned int*/
12ul /*unsigned long int*/
12UL /*unsigned long int*/
Rules for Constrcting integer constant
- Must have one or more digit without a decimal point.
- Can be either positive or negative. If no sign precedes the constant, it is positive
- No comma or blanks allowed within an integer constant
- Integer constants are by default int.
Example of invalid integer constants:
1.5 /*Not Valid*/
5+ /*Not valid */
1,5 /*Not valid */
Real or Floating Point (Float) constants
A floating point constant has integer part, decimal and fractional part. A floating point constant can also be represented in exponential form.
+123.50 /* 123 is decimal part, 50 is fractional part 8*/
+3.42e-4 /* In fractional form : 0.000342 */
Rules for constructing floating point constants
- It Must have at least one digit and only one decimal point.
- It Can be either positive or negative. If no sign precedes the constant, it is positive.
- No comma or blanks allowed within a floating constant.
- In Exponential form, ‘e’ can be either in lower case or upper case.
- Floating-point constants default to type double.
- By using the suffixes f or l (or F or L ), the constant can be specified as float or long double, respectively.
1.1.1 /*Not valid : two decimal points*/
1,1.1 /*Not Valid : comma not allowed */
5.4e5L /*Not Valid : L not allowed in exponential form*/
3.41E /*Not Valid : no exponent after E*/
A 'character constant' is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas.
Example : 'A' 'g' '1' '=' or '\t'
char datatype variables can be used to store character constants. The length of character constant is always 1 byte.
Rules for constructing character constants
- Single alphabet, digit or special symbol.
- Should be enclosed within single inverted commas.
- Since characters are internally treated as integers, the ASCII values of character is what is stored internally.For example ASCII for ‘A’ is 65.
- Range : -128 to 127
- Example: ‘C’,‘7’, Escape sequence like ‘\n’