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
Integer constant: A whole number such as 42. Forms such as 42U or 42LL specify an unsigned or long long form.
Floating constant: A value such as 3.5. It has type double by default; 3.5f has type float.
Character constant: A character in single quotes, such as A written as 'A'. An ordinary character constant has type int in C.
String literal: Characters in double quotes, such as "CodeBhavya". A terminating null character marks the end of the string.
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
Form
Example
Meaning
Integer constant
42, 42U, 0x2A
Whole-number constant; suffixes and bases affect type and interpretation.
Floating constant
3.5, 3.5f
double 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 constant
enum { 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.