- What are constants and literals in C
- What are integer literals and their types
- What are float literals
- What are character literals
- What are string literals
- What are boolean literals
What are Constants and Literals?
A constant is a name given to a variable whose value cannot be changed during program execution. Think of it like a fixed value that stays the same throughout your program.
Literals represent fixed values that cannot be modified. They are the actual values you write in your code. Generally, both terms — constants and literals — are used interchangeably.
💡 Key Difference: Constants are named (like PI), while literals are the actual values (like 3.14).
You can define constants in C using two methods:
- Using
constkeyword:const double PI = 3.14; - Using
#definepre-processor:#define PI 3.14
Example:
const double PI = 3.14; // Valid
PI = 2.9; // Error — cannot change a constant
C language has five types of literals:
- Integer Literals
- Float Literals
- Character Literals
- String Literals
- Boolean Literals
1. Integer Literals
Integer literals are numbers that do not have a decimal point or an exponential part. They are used to store integer values only.
An integer literal can be a decimal, octal, or hexadecimal constant:
- Decimal: No prefix —
85 - Octal: Prefix
0—0213 - Hexadecimal: Prefix
0xor0X—0x4b
Integer literals can also have suffixes:
uorU— unsignedlorL— longulorUL— unsigned long
Examples of Integer Literals:
85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
⚠️ Invalid Examples:
0898 /* Illegal: 8 and 9 are not octal digits */
032UU /* Illegal: cannot repeat a suffix */
2. Float Literals
Float literals are numbers that have a decimal point or an exponential part. They can be represented in decimal form or exponential form.
When representing literals in decimal form, you must include the decimal point, the exponent, or both.
The signed exponent is introduced by e or E.
Examples of Float Literals:
3.14159 /* Legal */
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */
3. Character Literals
A character literal is used to represent a single character. It contains a sequence of characters or escape sequences enclosed in single quotation marks (' ').
Examples:
'x' /* plain character */
'\t' /* escape sequence */
'\u02C0' /* universal character */
L'c' /* wide character literal */
The char type is used to store narrow-character literals (single-byte characters).
4. String Literals
A string literal is a sequence of characters enclosed in double quotation marks (" ").
String literals are used to represent a sequence of characters that form a null-terminated string.
Examples of String Literals:
"Best" /* string constant */
"" /* null string constant */
" " /* string of six spaces */
"x" /* string with a single character */
"Earth is round\n" /* prints string with a newline */
5. Boolean Literals
Boolean literals represent logical values and allow only two values:
- true — represents a true boolean value
- false — represents a false boolean value
Examples:
boolean b = true;
boolean d = false;
Boolean values are not case-sensitive — they can be written in uppercase or lowercase.
🎯 Key Takeaway: Constants are fixed values that cannot be changed during program execution. Literals are the actual values written in code. Understanding both is essential for writing clean, maintainable C programs.
Frequently Asked Questions About Constants and Literals
1. What is the difference between a constant and a variable?
A variable can change its value during program execution. A constant cannot change its value once assigned.
2. What is the difference between const and #define?
const is a keyword that creates a typed constant at compile time. #define is a pre-processor directive that performs text substitution before compilation.
3. What are the different types of literals in C?
C has five types of literals: integer, float, character, string, and boolean literals.
4. Can I change the value of a constant?
No. Once a constant is defined, its value cannot be changed. Attempting to do so will result in a compilation error.
💡 Tip: Use constants for values that don't change — like PI, GRAVITY, or maximum array sizes. This makes your code more readable and easier to maintain.