MCS-201 · Unit 2

Data Types, Operators and Expressions

Section 4 of 11

Block 1 · Unit 2Data Types, Operators and Expressions10 min · Section 4 of 11

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

BaseHow it is writtenValid examplesValue in decimal
Decimal (base 10)Digits 0–9, not starting with 0 (except 0 itself)443, 32767as written
Octal (base 8)Leading 0, then digits 0–7017, 0743017 = 15
Hexadecimal (base 16)Leading 0x or 0X, then 0–9 and A–F0x1F, 0XABCD0x1F = 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:

0178=1×8+7=151F16=1×16+15=31017_8 = 1 \times 8 + 7 = 15 \qquad \text{1F}_{16} = 1 \times 16 + 15 = 31

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.5e3 means 2.5×1032.5 \times 10^{3}, and 2e-5 means 2×10−52 \times 10^{-5}

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:

EscapeMeaningEscapeMeaning
\nNew line\\Backslash
\tHorizontal tab\'Single quote
\rCarriage return\"Double quote
\bBackspace\0Null character (code 0)
\aAlert (beep)\?Question mark
\fForm feed\vVertical 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 1 · multiple choice

What is the decimal value of the octal constant 017?

Show answer

Answer: B. 15

1×8+7=151 \times 8 + 7 = 15. It is tempting to read it as 17, but the leading zero makes it octal.

Question 2 · multiple choice

Which of these is a valid hexadecimal constant?

Show answer

Answer: D. 0xABCD

A hexadecimal constant needs 0x or 0X and digits 0–9 or A–F. 0BEF lacks the x, and G and H are not hexadecimal digits.

Question 3 · multiple choice

What is 0777.77?

Show answer

Answer: C. A valid floating constant with value 777.77

The decimal point makes it a floating constant, and leading zeros are allowed in floating constants. The octal rule applies only to integers.

Question 4 · multiple choice

How many bytes of memory does the string constant "CAT" occupy?

Show answer

Answer: B. 4

Three characters plus the hidden null character \0 make 4 bytes. Forgetting the \0 gives the tempting answer 3.

Question 5 · multiple choice

What does printf("%d\n", 'B'); print on an ASCII system?

Show answer

Answer: D. 66

%d prints the integer code of the character, and 'B' is 66 in ASCII. 98 is the code of lower-case 'b'.

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 2×16+10=422 \times 16 + 10 = 42. 052: the digits are 5 and 2, so the value is 5×8+2=425 \times 8 + 2 = 42. Both constants represent the same number, 42.

↑ ↓ move↵ openTab switch type