In this tutorial you will learn about the C Constants and its application with practical example.
In C Programming Language constant is an entity with a fixed value, and this value does not change throughout the execution of the program. C constants are also known as ‘Literals’. There are four basic types of constants in C Programming Language.
Integer Constants
An integer constant can only hold integer quantity which is a sequence of whole numbers or digits.
- Integer constant can not have a decimal point or fractional part.
- Blanks and commas are not allowed within an integer constant.
- An integer constant can be either +ve or -ve.
- The constant must lie within the range of the declared data type (including qualifiers long, short, etc.).
An integer constant can be either Decimal, Hexa Decimal, or Octal.
A decimal integer constant consists of any combination of digits taken from the set 0 through 9. If the decimal constant contains two or more digits, the first digit can not be 0 (Zero).
Example:
12, 15, 456 etc.
An octal integer constant is a combination of digits taken from the set 0 through 7. In octal representation, the first digit must be a 0(Zero), in order to identify the constant as an octal number.
Example:
0, 05, 054
A hexadecimal integer constant must begin with either 0x or 0X. It can then be followed by any combination of digits taken from the set 0 through 9 and alphabets from A to F (either upper-case or lower-case are valid).
Example:
0x65F, 0X7A
Floating Point Constants
A floating-point or real constant contains a decimal point or an exponent.
- A real constant must have at least one digit each to the left and right of the decimal point.
- It can be either +ve or -ve.
- Commas and blank space are not allowed within a real constant.
A real constant can be represented in two forms: Factorial form or Exponential form.
A real constant in a fractional form must have at least one digit each to the left and right of the decimal point.
Example:
15.75, -2.25
A floating-point in exponent form consists of a mantissa and an exponent.
Example:
-2.5e-3, 25E-4
Character Constants
A character constant is a single character, enclosed in single quotation marks. It can be a single alphabet, a digit, or a special symbol. The maximum length of a character constant is one character.
Example:
‘A’, ‘5’, ‘$’
String Constants
A string constant is a sequence of characters enclosed in double quotes. It may contain letters, digits, special characters, and blank spaces.
Example:
“HELLO”, “world”