2.4Constants
Covers IGNOU MCS-201 §2.9, 2.9.1, 2.9.2, 2.9.3, 2.9.4
A constant is a value written directly in the program, such as 100, 3.14 or 'A'. C has precise rules for how each kind of constant is written, and small details, such as a leading zero, change the meaning completely. This section covers integer, floating, character and string constants.
Constants and their kinds
C has four basic kinds of constant: integer, floating point, character and string. For numeric constants, no commas or spaces are allowed, and a minus sign may be placed in front.
Integer constants
| Base | How it is written | Valid examples | Value in decimal |
|---|---|---|---|
| Decimal (base 10) | Digits 0–9, not starting with 0 (except 0 itself) | 443, 32767 | as written |
| Octal (base 8) | Leading 0, then digits 0–7 | 017, 0743 | 017 = 15 |
| Hexadecimal (base 16) | Leading 0x or 0X, then 0–9 and A–F | 0x1F, 0XABCD | 0x1F = 31 |
A leading zero makes a constant octal, so 0900 is invalid: 9 is not an octal digit. Suffixes change the type: U or u for unsigned (50000U), L or l for long (1234567889L), and LL for long long.
To convert, multiply each digit by a power of the base:
Floating-point constants
A floating constant has a decimal point, an exponent, or both:
- Decimal notation:
3.14,-0.5,0.,.09 - Exponent (scientific) notation:
2.5e3means , and2e-5means
A floating constant has type double unless it has the suffix f (float) or L (long double). Leading zeros do not matter in floating constants: 0777.77 is simply 777.77.
Character constants and escape sequences
A character constant is a single character in single quotes, such as 'A', 'x' or '7'. Each character is stored as its integer code; on ASCII systems 'A' is 65, 'a' is 97 and '0' is 48. Note that '7' (code 55) is not the number 7.
An escape sequence is a backslash followed by a character, used to write characters that cannot be typed directly:
| Escape | Meaning | Escape | Meaning |
|---|---|---|---|
\n | New line | \\ | Backslash |
\t | Horizontal tab | \' | Single quote |
\r | Carriage return | \" | Double quote |
\b | Backspace | \0 | Null character (code 0) |
\a | Alert (beep) | \? | Question mark |
\f | Form feed | \v | Vertical tab |
String constants
A string constant is a sequence of characters in double quotes, such as "red" or "Blue Sea". C automatically adds a hidden null character \0 at the end to mark where the string stops. So "A" takes 2 bytes ('A' and '\0'), while 'A' is a single character.
Key takeaways
- Integer constants can be decimal, octal (leading 0) or hexadecimal (leading 0x), with optional U and L suffixes.
- Floating constants have a decimal point or exponent; their default type is
double. - Character constants use single quotes and are stored as integer codes; escape sequences start with
\. - String constants use double quotes and end with a hidden
\0.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Convert the hexadecimal constant 0x2A and the octal constant 052 to decimal, showing the working.
Show worked answer
0x2A: the digits are 2 and A (= 10), so the value is . 052: the digits are 5 and 2, so the value is . Both constants represent the same number, 42.