There are mainly five types of Literals in C. These are as follows:
Here, we will discuss these literals one by one.
1) Integer Literals
In C programming , an integer literal is a numeric literal that represents only integer type values. It represents the value neither in the fractional nor exponential part. They can be preceded by a 0x or 0X designation, where they lie in base 10, base 8, or base 16.
Moreover, they can also be marked with positive or negative modifiers, such as U (Unsigned), L (Long), or LL (Long Long). Valid examples include 100, 075, or 0x1F. They are extensively used in array sizes, loops, counters, and other numerical operations that require whole-number constants.
For Example:
100 // Decimal
0x1F // Hexadecimal (prefix: 0x)
075 // Octal (prefix: 0)
Types of Integer Literals (Based on Number System)
There are several types of integer literals in C. Some of them are as follows:
Decimal Number (Base 10)
It is defined by representing the digits between 0 to 9. For example, 45, 67, etc.
Octal Number (Base 8)
It is defined as a number in which 0 is followed by digits such as 0,1,2,3,4,5,6,7. For example, 012, 034, 055, etc.
Hexadecimal Number (Base 16)
It is defined as a number in which 0x or 0X is followed by the hexadecimal digits (i.e., digits from 0 to 9, alphabetical characters from (a-z) or (A-Z)).
Suffixes (Type Modifiers)
Several suffix literals in C are as follows:
| Suffix | Meaning | Example |
|---|---|---|
| u or U | Unsigned | 100U |
| l or L | Long | 100L |
| ul or UL | Unsigned Long | 100UL |
| ll or LL | Long Long | 100LL |
| ull or ULL | Unsigned Long Long | 100ULL |
Here, we will discuss these suffix literals in C one by one.
Unsigned Integer Literals
In C, it is a sign qualifier that represents the type of the integer as unsigned. An unsigned qualifier contains only positive values. It ends with the suffix U or u. It also ensures the compiler treats the value as an unsigned int.
Long Integer Literals
In C, long integer literals are a size qualifier that specifies the size of the integer type as long. It is usually used in 32-bit or 64-bit system.
Unsigned Long Integer Literals
In C programming, these literals are used to represent a non-negative whole number that fits inside the range of the unsigned long data type. It can be very useful if we want to store large positive integers without using any negative values. It ends with suffix UL, ul, etc.
Long Long Integer Literals
In C, it is used to represent whole numbers that are larger than long int. It can be useful if we need to work with very large integer values, especially on systems where a long is 32 bits and cannot hold 64-bit values. It ends with suffix LL or ll.
Unsigned Long Long Literals
In C, these literals are constants that are used to represent only non-negative whole numbers with a very large range. These are mainly utilized when we need to store values larger than what the signed integers can hold. It ends with the suffix ULL, ull.
C Example for Integer literal
Let us take an example to illustrate the integer literal in C.
Example
#include <stdio.h>
int main() { //main function
int decimal = 100; // Decimal (Base 10)
int octal = 012; // Octal (Base 8) = 10 in decimal
int hex = 0x1A; // Hexadecimal (Base 16) = 26 in decimal
unsigned int u = 30u; //unsigned int
long l = 22L; //long int
long long ll = 35LL; //long long int
printf("Decimal: %d\n", decimal);
printf("Octal (012): %d\n", octal);
printf("Hexadecimal (0x1A): %d\n", hex);
printf("Unsigned: %u\n", u);
printf("Long: %ld\n", l);
printf("Long Long: %lld\n", ll);
return 0;
}
Output:
Decimal: 100
Octal (012): 10
Hexadecimal (0x1A): 26
Unsigned: 30
Long: 22
Long Long: 35
Explanation:
In this example, we illustrate the different types of integer literals, such as decimal, octal, and hexadecimal values, along with variations like unsigned int, long int, and long long int. After that, it prints each value using suitable format specifiers , such as %d, %u, %ld, and %lld for proper output.
2) Floating Literals
In C programming, it is a literal that contains only floating-point values or real numbers. These real numbers contain the number of parts, such as the integer part, real part, exponential part, and fractional part. The floating-point literal must be specified either in decimal or in exponential form. Scientific notation, such as 2.5e2 (which equals 250) or standard decimal notation, such as 3.14, can be used to express them. If we are using the floating literal, we have to remember the following points:
Decimal form
In the decimal form, we must contain either a decimal point, an exponential part, or both. If it does not contain either of these, then the compiler will throw an error. The decimal notation can be prefixed by either '+' or '-' symbol that specifies the positive and negative numbers.
Examples of float literals in decimal form are:
1.2, +9.0, -4.5
Exponential form
The exponential form is useful when we want to represent a number which is having a large magnitude. It contains two parts, i.e., mantissa and exponent. For example, the number is 2340000000000, and it can be expressed as 2.34e12 in an exponential form.
Examples of real literals in exponential notation are:
+1e23, -9e2, +2e-25
C Example for Floating Point Literal
Let us take an example to illustrate the floating point literals in C.
Example
#include <stdio.h>
int main() { //main function
float f = 3.14f; // Float with suffix
double d = 2.71828; // Double (default type)
double sci = 1.5e3; // Scientific notation = 1500
printf("Float: %f\n", f);
printf("Double: %lf\n", d);
printf("Scientific: %lf\n", sci);
return 0;
}
Output:
Float: 3.140000
Double: 2.718280
Scientific: 1500.000000
Explanation:
In this example, we demonstrate the floating-point literals a float (suffix f), a double (default for decimals), and a value in scientific notation (1.5e3, which equals 1500). After that, it uses %f and %lf format specifiers to correctly display the values.
3) Character Literals
In C programming, a character literal contains a single character enclosed within single quotes. If multiple characters are assigned to the variable , we need to create a character array. If we try to store more than one character in a variable, a warning of a multi-character character constant will be generated.
Representation of character literal
There are several ways to represent the character literal in C. Some of them are as follows:
- It can be represented by specifying a single character within single quotes. For example, 'a', 'b', etc.
- We can specify the escape sequence character within single quotes to represent a character literal. For example, '\n', '\a', '\b'.
- We can also use the ASCII in an integer to represent a character literal. For example, the ascii value of 65 is 'A'.
- The octal and hexadecimal notations can be used as an escape sequence to represent a character literal. For example, '\023', '\0x12'.
For Example:
'a'
'Z'
'\n' // Newline character (escape sequence)
Example for Character Literals in C
Let us take an example to illustrate the character literals in C.
Example
#include <stdio.h>
int main() { //main function
char ch = 'A'; // Character literal
char newline = '\n'; // Escape character
printf("Character: %c\n", ch);
printf("Newline character is executed now%cEnd\n", newline);
return 0;
}
Output:
Character: A
Newline character is executed now
End
Explanation:
In this example, we demonstrate the character literals, such as a regular character ('A') and an escape character ('\n' for newline). After that, it prints the character using the %c format specifier and displays how the newline character moves the output to a new line during execution.
4) String Literals
In C programming, a string literal represents multiple characters enclosed within double quotes. It contains an additional character, i.e., '\0' (null character), which gets automatically inserted. This null character specifies the termination of the string. We can use the '+' symbol to concatenate two strings.
As string literals are constant, they cannot be modified during program execution. Moreover, string literals may include escape sequences like \n, \t, and \\. Although character arrays or pointers are used in C for string manipulation, string literals serve as the fundamental building blocks for these operations.
For Example:
"Hello, World!"
"123"
"New\nLine"
Example for String Literals in C
Let us take an example to illustrate the string literals in C.
Example
#include <stdio.h>
int main() { //main function
char str1[] = "Hello, C!"; // String literal
char str2[] = "Line1\nLine2"; // String with escape sequence
printf("String 1: %s\n", str1);
printf("String 2:\n%s\n", str2);
return 0;
}
Output:
String 1: Hello, C!
String 2:
Line1
Line2
Explanation:
In this example, we demonstrate the string literals, where str1 holds a simple string and str2 that contains an escape sequence (\n) to print on a new line. After that, the %s format specifier is utilized in the printf function to print the string contents.
5) Boolean Literals
The C99 standard added boolean literals, which represent truth values, by including the header file. The Bool is the type of the two boolean literals: true (non-zero) and false (zero). These literals help to improve the code readability and clarify logical expressions. If (isvalid == true) is more expressive, it returns 1 or 0. Writing conditionals and return values from logical functions is now easier and more error-proof due to the addition of a boolean type. It is defined in the <stdbool.h> header file.
For Example:
true
false
Example for Boolean Literals (C99 and Later) in C
Let us take an example to illustrate the Boolean literals in C.
Example
#include <stdio.h>
#include <stdbool.h>
int main() { //main function
bool isOnline = true;
bool isEmpty = false;
printf("Online: %d\n", isOnline); // true = 1
printf("Empty: %d\n", isEmpty); // false = 0
return 0;
}
Output:
Online: 1
Empty: 0
Explanation:
In this example, we demonstrate the Boolean literals in C. Here, we have taken the two bool functions, i.e., isOnline and isEmpty, as true and false. After that, we use the printf function to print the value of the isOnline and isEmpty functions, where true is represented as 1 and false is represented as 0.
Differences between All the Literals in C
There are several differences between these literals in C. Some of them are as follows:
| Types of Literals | Example | DataType | Usage | Notes/Suffix |
|---|---|---|---|---|
| Integer (Decimal) | 100 | Int | Whole numbers in base 10 | Its default type is int. |
| Integer (Octal) | 075 | Int | Whole numbers in base 8 (prefix 0) | Use 0 prefix to represent octal |
| Integer (Hexadecimal) | 0x1A | Int | Whole numbers in base 16 (prefix 0x) | Use 0x or 0X prefix |
| Unsigned Integer | 100U | Unsigned int | Non-negative values only | Suffix U or u |
| Long Integer | 100000L | Long int | Large Integer values | Suffix L or l |
| Unsigned long | 10000UL | Unsigned long int | Non-negative large integers | Suffix UL or ul |
| Long Long Integer | 123456789LL | long long int | Very large integer values | Suffix LL and ll |
| Unsigned Long Long | 123456789ULL | Unsigned long long int | Suffix | Suffix ULL and ull |
| Floating-Point (Float) | 3.14f | Float | Decimal values with fractional parts | Suffix f or F |
| Floating-Point (Double) | 2.71828 | Double | More precision than float | Default type for floating-point literals |
| Scientific Notation | 1.5e3 | Double | Exponential form (1.5 × 10^3 = 1500) | Use e or E for exponent |
| Character Literal | 'A', '\n' | Char | Single character enclosed in single quotes | Escape characters allowed (\n, \t, etc.) |
| String Literal | "Hello" | char[] (array of chars) | Text strings enclosed in double quotes | Null-terminated (\0) automatically |
| Boolean Literal | True, false | Bool (from <stdbool.h>) | Logical true/false values | Logical true/false values |
Advantages of Literals in C
Several advantages of literals in C are as follows:
- Assigning constant values in the code is made very easy through literals. Expressions like "int x = 10" are simple to write, along with understanding the code.
- Literals serve to make assembly code simpler to understand and improve efficiency. The reason is that assembly code has a set value.
- Literals require no pre-declaration, whereas variables do; hence there is minimal code overhead for one-time values.
- Versatile individuals can handle a wide range of use cases, including math operations, messages, conditions, and more, due to its various literals, which include integer, float, character, and string.
- Literals improve the readability of logic flows with fixed values and are essential in loops and conditional statements (if, switch).
Disadvantages of Literals in C
Several disadvantages of Literals in C are as follows:
- Using raw literals rather than named constants (e.g., 3.14, 1000) can make code more difficult to maintain or modify later.
- Literals are fixed at compile time and cannot be changed during execution; therefore, dynamic behavior is limited.
- The use of the same literal in many areas causes code duplication and can lead to inconsistencies if changes are required.
- Misusing suffixes or literal types might cause unexpected behavior or overflow.
- Literals lack context. For example, 60 could mean minutes, seconds, or another unit; constants like MAX_SPEED are clearer.
Conclusion
In conclusion, literals are basic C building blocks that are used directly in code to represent fixed values. They enhance program clarity, boost efficiency, and simplify logic control and variable initialization in addition to a wide variety of types, including characters, strings, integers, and floating-point numbers. C provides a varied set of literals to accommodate several programming needs.