Constants
Constants in C are fixed value that does not change during the execution of a program. Constants can be of any of the basic data types. C supports several types of constants in C language as
C constants
a. Numeric Constants
i. Integer Constant
1. Decimal Integer constant
2. Octal integer constant
3. Hexadecimal Integer constant
ii. Real Constant
b. Character Constants
i. Single Character Constant
ii. String Constant
c. Backslash Character constants
d. Symbolic constants
Numeric constants: There are two types of numeric constants,
Integer constants
There are three types of integer constants namely,
a) Decimal integer constant
b) Octal integer constant
c) Hexadecimal integer constant
Decimal Integer constant (base 10)
Invalid:
12,245 - Illegal character (,)
10 20 30 - Illegal character (blank space)
Octal Integer Constant (base 8)
Invalid:
0786 - Illegal digit 8
123 - Does not begin with zero
01.2 - Illegal character (.)
Hexadecimal integerconstant
Invalid Hexadecimal Integer Constant:
0xefg - Illegal character g
123 - Does not begin with 0x
Unsigned integer constant: An unsigned integer constant specifies only positive integer value. It is used only to count things. This constant can be identified by appending the letter u or U to the end of the constant.
Valid: 0u 1U 65535u 0x233AU
Invalid: -123 - Only positive value
Long integer constant: A long integer constant will automatically be generated simply by specifying a constant that exceeds the normal maximum value. It is used only to count things. This constant can be identified by appending the letter l or L to the end of the constant.
Valid: 0l23456L 0x123456L -123456l
Invalid: 0x1.2L - Illegal character (.)
Short integer constant: A short integer constant specifies small integer value. This constant can be identified by appending the letter s or S to the end of the constant.
Valid: 123s -456 32767S
Invalid:
12,245 - Illegal character (,)
10 20 30 - Illegal character (blank space)
Note: - A sign qualifier can be appended at the end of the constant. Usually suffixes(s or S, u or U, l or L) are not needed. The compiler automatically considers small integer constants to be of type short and large integer constants to be of type long.
Rules for constructing Integer constants
i. An integer constant must have at least one digit.
ii. It must not have a decimal point.
iii. It can be either positive or negative.
iv. If no sign precedes an integer constant, it is assumed to be positive.
v. Commas or blanks are not allowed within an integer constant.
Constants in C are fixed value that does not change during the execution of a program. Constants can be of any of the basic data types. C supports several types of constants in C language as
C constants
a. Numeric Constants
i. Integer Constant
1. Decimal Integer constant
2. Octal integer constant
3. Hexadecimal Integer constant
ii. Real Constant
b. Character Constants
i. Single Character Constant
ii. String Constant
c. Backslash Character constants
d. Symbolic constants
Numeric constants: There are two types of numeric constants,
- Integer constants
- Real or floating-point constants
Integer constants
- Any whole number value is an integer.
- An integer constant refers to a sequence of digits without a decimal point.
- An integer preceded by a unary minus may be considered to represent a negative constant
There are three types of integer constants namely,
a) Decimal integer constant
b) Octal integer constant
c) Hexadecimal integer constant
Decimal Integer constant (base 10)
- It consists of any combinations of digits taken from the set 0 through 9, preceded by an optional – or + sign.
- The first digit must be other than 0.
- Embedded spaces, commas, and non-digit characters are not permitted between digits.
Invalid:
12,245 - Illegal character (,)
10 20 30 - Illegal character (blank space)
Octal Integer Constant (base 8)
- It consists of any combinations of digits taken from the set 0 through 7.
- If a constant contains two or more digits, the first digit must be 0.
- In programming, octal numbers are used.
Invalid:
0786 - Illegal digit 8
123 - Does not begin with zero
01.2 - Illegal character (.)
Hexadecimal integerconstant
- It consists of any combinations of digits taken from the set 0 through 7 andalso a through f (either uppercase or lowercase).
- The letters a through f (or A through F) represent the decimal quantities 10 through 15 respectively.
- This constant must begin with either 0x or 0X.
- In programming, hexadecimal numbers are used.
Invalid Hexadecimal Integer Constant:
0xefg - Illegal character g
123 - Does not begin with 0x
Unsigned integer constant: An unsigned integer constant specifies only positive integer value. It is used only to count things. This constant can be identified by appending the letter u or U to the end of the constant.
Valid: 0u 1U 65535u 0x233AU
Invalid: -123 - Only positive value
Long integer constant: A long integer constant will automatically be generated simply by specifying a constant that exceeds the normal maximum value. It is used only to count things. This constant can be identified by appending the letter l or L to the end of the constant.
Valid: 0l23456L 0x123456L -123456l
Invalid: 0x1.2L - Illegal character (.)
Short integer constant: A short integer constant specifies small integer value. This constant can be identified by appending the letter s or S to the end of the constant.
Valid: 123s -456 32767S
Invalid:
12,245 - Illegal character (,)
10 20 30 - Illegal character (blank space)
Note: - A sign qualifier can be appended at the end of the constant. Usually suffixes(s or S, u or U, l or L) are not needed. The compiler automatically considers small integer constants to be of type short and large integer constants to be of type long.
Rules for constructing Integer constants
i. An integer constant must have at least one digit.
ii. It must not have a decimal point.
iii. It can be either positive or negative.
iv. If no sign precedes an integer constant, it is assumed to be positive.
v. Commas or blanks are not allowed within an integer constant.