Integer literal is a type of literal for an integer whose value is directly represented in source code. For example, in the assignment statement x = 1, the string 1 is an integer literal indicating the value 1, while in the statement x = 0x10 the string 0x10 is an integer literal indicating the value 16(in decimal), which is represented by 10 in hexadecimal (indicated by the 0x prefix).
Further, in x = “1” the “1” is a string literal(not a character or an integer literal), because it is in quotes. The value of the string is 1, which happens to be an integer string.
Integer literals are expressed in two types i.e.,
- Prefixes which indicates the base. For example, 0x10 indicates the value 16 in hexadecimal having prefix 0x.
- Suffixes which indicates the type. For example, 12345678901234LL indicates the value 12345678901234 as an long long integer having suffix LL.
Prefixes: They are basically represent in four types.
- Decimal-literal(base 10):- a non-zero decimal digit followed by zero or more decimal digits(0, 1, 2, 3, 4, 5, 6, 7, 8, 9). For example, 56, 78.
- Octal-literal(base 8):- a zero followed by zero or more octal digits(0, 1, 2, 3, 4, 5, 6, 7). For example, 045, 076, 06210.
- Hex-literal(base 16):- 0x or 0X followed by one or more hexadecimal digits(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F). For example, 0x23A, 0Xb4C, 0xFEA.
- Binary-literal(base 2):- 0b or 0B followed by one or more binary digits(0, 1). For example, 0b101, 0B111.
Suffixes: They are represented in many ways according to their data types.
- int:- No suffix are required because integer constant are by default assigned as int data type.
- unsigned int: character u or U at the end of integer constant.
- long int: character l or L at the end of integer constant.
- unsigned long int: character ul or UL at the end of integer constant.
- long long int: character ll or LL at the end of integer constant.
- unsigned long long int: character ull or ULL at the end of integer constant.
Output: 213 139 8506 5 1234567890123456789 12345678901234567890 12345678901234567890 1221300
Digit separator: In C++, integer literals may contain digit separators to allow digit grouping into more readable forms. This is particularly useful for bit fields, and makes it easier to see the size of large numbers (such as a million) at a glance by subitizing rather than counting digits. It is also useful for numbers that are typically grouped, such as credit card number or social security numbers.[a] Very long numbers can be further grouped by doubling up separators.
Typically decimal numbers (base-10) are grouped in three digit groups (representing one of 1000 possible values), binary numbers (base-2) in four digit groups (one nibble, representing one of 16 possible values), and hexadecimal numbers (base-16) in two digit groups (each digit is one nibble, so two digits are one byte, representing one of 256 possible values). Numbers from other systems (such as id numbers) are grouped following whatever convention is in use.
Output: 12345678901245 12345678901245 142 1221300
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- ASCII NUL, ASCII 0 ('0') and Numeric literal 0
- Raw string literal in C++
- Assigning an integer to float and comparison in C/C++
- Storage of integer and character values in C
- How to Read and Print an Integer value in C
- How to Read and Print an Integer value in C++
- Declare a C/C++ function returning pointer to array of integer pointers
- Integer Promotions in C
- How to concatenate two integer arrays without using loop in C ?
- Extended Integral Types (Choosing the correct integer size in C/C++)
- A creative C++ program to Zoom digits of an integer
- Check for integer overflow on multiplication
- Input an integer array without spaces in C
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- fesetround() and fegetround() in C++ and their application
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Get first and last elements from Array and Vector in CPP
- Similarities and Differences between Ruby and C language
- Code Optimization Technique (logical AND and logical OR)