## 27 Aug Constants in C Language

Constants in C language are the values that cannot be modified once they are defined. After definition, they are fixed in a program. There are primarily three types of constants, namely integer, real and character constants.

Details about these are given as follows:

## Integer Constants

The different rules for constructing integer constants are:

- There should be at least one digit in the integer constant.
- There should be no decimal points.
- The integer constant can be positive or negative.
- If there is no sign specified before the integer constant, it is assumed to be positive.
- There should be no commas and blanks in the integer constant.

Some examples of integer constants are 23,-55,+55 etc.

## Real Constants

Real constants or floating point constants can be written in two forms: Fractional form or Exponential form.

The different rules for constructing real constants in fractional form are:

- There should be at least one digit in the real constant.
- There should be a decimal point.
- The real constant can be positive or negative.
- If there is no sign specified before the real constant, it is assumed to be positive.
- There should be no commas and blanks in the real constant.

Some examples of real constants in fractional form are 23.5, +66.87, -75.23 etc.

The exponential form is usually used when the real constants are either too large or too small. In this form, the constants are represented in two parts. The part before e is called mantissa and the part after e is called exponent.

The different rules for constructing real constants in exponential form are:

- e should separate the mantissa and exponent.
- The mantissa can be positive or negative.
- If there is no sign specified before the mantissa, it is assumed to be positive.
- There should be at least one digit in the exponent. It is assumed to be positive by default.

Some examples of real constants in exponential form are +2.3e5, -6.6e-7, -7.5e3 etc.

## Character Constants

These consist of a single character such as an alphabet, digit or special character enclosed within inverted commas.

The different rules for constructing character constants are:

- Maximum length of a character constant is one character.
- The character constant should be enclosed with left pointing inverted commas.
- Any alphabet, digit or special character can be a character constant.

Some examples of character constants in are ‘A’, ‘7’, ‘%’ etc.

A coding example of constants in C language is:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include < stdio.h > int main(void) { const int a = 50; // Integer constant const float b = 33.25; // Real constant const char c = 'C'; // Character constant printf("Constants in C - Studyopedia\n"); printf("----------------------------\n"); printf("%d\n", a); printf("%f\n", b); printf("%c\n", c); return 0; } |

The output obtained for the above code is:

## No Comments