Skip to study material
← Question index

QUESTION 48 · C fundamentals

Explain constants, literals and symbolic constant names in C.

5 marks

Answer in brief

A constant written in the source represents a fixed value. Its form tells the compiler what kind of value it is.

Types of values in source code

  1. Integer constant: A whole number such as 42. Forms such as 42U or 42LL specify an unsigned or long long form.
  2. Floating constant: A value such as 3.5. It has type double by default; 3.5f has type float.
  3. Character constant: A character in single quotes, such as A written as 'A'. An ordinary character constant has type int in C.
  4. String literal: Characters in double quotes, such as "CodeBhavya". A terminating null character marks the end of the string.
  5. Enumeration constant: A named integer value introduced by an enum declaration.

Named constants

A name can explain a value that is used repeatedly. #define MAX_STUDENTS 60 replaces the macro name during preprocessing. const double tax_rate = 0.05; creates a typed object that cannot be modified through that object. These are different mechanisms; const does not automatically make an object an integer constant expression in C.

Character and string example

'A' is a character constant. "A" is a string literal containing A and a null character, so its array size is 2 C bytes. Single quotes and double quotes are not interchangeable.

Important points

Do not try to assign a new value to a literal or modify a string literal. A writable character array initialised from a string literal has its own elements and is different from the literal itself. Use meaningful constant names to avoid repeating unexplained numbers in a program.

Reference table

↔ Scroll horizontally on a small screen
Values written in source
FormExampleMeaning
Integer constant42, 42U, 0x2AWhole-number constant; suffixes and bases affect type and interpretation.
Floating constant3.5, 3.5fdouble by default; f requests float.
Character constant'A', '\n'An ordinary character constant has type int in C.
String literal"CodeBhavya"An array of characters including a terminating null character.
Enumeration constantenum { LIMIT = 10 };Introduces an integer constant name.

Key points

  • A macro replacement and a const object are not the same language feature.
  • The type of a literal depends on its form and suffix.