Lesson 04·C Programming
Constants
Fixed values that remain immutable throughout program execution
Constants in C
A constant is an entity whose value does not change during the execution of a program.
In C, constants can be defined using the const keyword or preprocessor directives.
C Example
const int a = 20; // Value of 'a' cannot be modifiedNumber Notation Formats
C supports different numeric representation notations:
- Octal constants are written with a leading zero (0) — e.g., 015
- Hexadecimal constants are written with a leading 0x or 0X — e.g., 0x15
- Long constants are written with a trailing L or l — e.g., 15L
1. Real Constant
- Must have at least one digit.
- Must have a decimal point which may be preceded by a positive (+) or negative (-) sign.
- Use of blank space and commas is not allowed within real constants.
- Example: +194.143, -416.41, 3.14159
2. Integer Constant
- Must have at least one digit.
- Should not contain any decimal point or fraction.
- Can be positive or negative (default is positive if unsigned).
- Use of blank spaces and commas is strictly prohibited.
- Example: 1990, 194, -394, 0
3. Character Constant
- Consists of a single alphabet, digit, or special symbol enclosed in single quotation marks.
- Maximum length of a character constant is exactly 1 character.
- Example: 'T', '9', '$', 'a'
4. String Constant
- A collection or sequence of characters enclosed in double quotation marks.
- May contain letters, numbers, special characters, and blank spaces.
- Automatically terminated by the null character (\0) in memory.
- Example: "Islamiah College, Vaniyambadi.", "Hello C", "1234"