Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Integer literal in C/C++ (Prefixes and Suffixes)
Integer literals in C are numeric values written directly in the source code to represent integer constants. They can be modified using prefixes to specify the base (decimal, octal, hexadecimal, binary) and suffixes to specify the data type (int, long, unsigned, etc.).
Integer literals are of two types −
- Prefixes − Indicate the number base. For example, 0x10 represents hexadecimal value 16.
- Suffixes − Specify the data type. For example, 123LL represents a long long integer.
Syntax
// Prefixes decimal_literal (no prefix) 0octal_literal (prefix 0) 0xhexadecimal (prefix 0x or 0X) 0bbinary (prefix 0b or 0B, C23) // Suffixes integer_literal[u|U][l|L|ll|LL]
Prefixes for Number Bases
Different prefixes specify the base of the number −
#include <stdio.h>
int main() {
printf("Decimal 213: %d\n", 213); /* Base 10 */
printf("Octal 0213: %d\n", 0213); /* Base 8 */
printf("Hex 0x213A: %d\n", 0x213A); /* Base 16 */
printf("Binary 0b101: %d\n", 0b101); /* Base 2 (C23) */
return 0;
}
Decimal 213: 213 Octal 0213: 139 Hex 0x213A: 8506 Binary 0b101: 5
Suffixes for Data Types
Suffixes specify the integer type and signedness −
#include <stdio.h>
int main() {
printf("Long: %ld\n", 1234567890123456789L);
printf("Long Long: %lld\n", 1234567890123456789LL);
printf("Unsigned: %u\n", 12345U);
printf("Unsigned Long Long: %llu\n", 12345678901234567890ULL);
return 0;
}
Long: 1234567890123456789 Long Long: 1234567890123456789 Unsigned: 12345 Unsigned Long Long: 12345678901234567890
Common Suffix Combinations
| Suffix | Type | Example |
|---|---|---|
| None | int | 123 |
| U or u | unsigned int | 123U |
| L or l | long | 123L |
| LL or ll | long long | 123LL |
| UL or ul | unsigned long | 123UL |
| ULL or ull | unsigned long long | 123ULL |
Conclusion
Integer literals in C provide flexible ways to represent numbers in different bases using prefixes and specify exact data types using suffixes. This ensures precise control over numeric values and their storage requirements in C programs.
Advertisements
