Integer literal in C/C++ (Prefixes and Suffixes)
Last Updated :
19 Jun, 2017
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.
Syntax
-
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.
CPP
// C++ program to demonstrate the use of
// integer literal
#include <iostream>
using namespace std;
int main()
{
// PREFIXES
cout << 213 << '\n' // decimal integer literal
<< 0213 << '\n' // Octal integer literal
<< 0x213A << '\n' // hexadecimal integer literal
<< 0b101 << '\n' // binary integer literal
// SUFFIXES
// long long literal
<< 1234567890123456789LL << '\n'
// unsigned long long literal
<< 12345678901234567890ull << '\n'
// automatic conversion of unsigned long long even
// without long long prefix
<< 12345678901234567890u;
return 0;
}
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.
CPP
// C++ program to demonstrate digit separator
#include <iostream>
using namespace std;
int main()
{
cout << 12345678901245LL <<'\n'
// long long int literal digit separator
<< 12'345'678'901'245LL <<'\n'
// binary literal digit separator
<< 0b1000'111'0 <<'\n'
// hexadecimal literal digit separator
<< 0X12A'2b4;
return 0;
}
Output:
12345678901245
12345678901245
142
1221300
Reference:-
https://en.wikipedia.org/wiki/Integer_literal
Similar Reads
match_results prefix() and suffix() in C++ The match_results::prefix() is an inbuilt function in C++ which is used to get the string which is preceding the matched string in the input target string.Syntax: smatch_name.prefix() Note: smatch_name is an object of match_results class.Parameters: This function accept no parameters.Return Value: T
2 min read
Print an Integer Value in C Printing an Integer in C is one of the most basic tasks. Input and output of the elements are essential for taking input from the user and then giving the output to the user. Here we are going to take an integer as input from the user with the help of the scanf() function and print that integer with
2 min read
Storage of integer and character values in C Integer and character variables are used so often in the programs, but how these values are actually stored in C are known to few. Below are a few examples to understand this: Taking a positive integer value as char: C #include <stdio.h> int main() { char a = 278; printf("%d", a); re
2 min read
Pre-increment and Post-increment in C/C++ In C/C++, Increment operators are used to increase the value of a variable by 1. This operator is represented by the ++ symbol. The increment operator can be classified into two types:Pre-Increment OperatorPost-Increment OperatorPre and Post Increment Operator in C/C++ The pre and post-increment ope
4 min read
Precedence of postfix ++ and prefix ++ in C/C++ In C/C++, precedence of Prefix ++ (or Prefix --) has same priority than dereference (*) operator, and precedence of Postfix ++ (or Postfix --) is higher than both Prefix ++ and *. If p is a pointer then *p++ is equivalent to *(p++) and ++*p is equivalent to ++(*p) (both Prefix ++ and * are right ass
4 min read
std::integer_sequence in C++ 14 When programming, loops, and iteration are often required by developers. Occasionally, looping through a range of numbers whose span is unknown becomes necessary std::integer sequence is useful in that scenario. By using std::integer_sequence provided by C++14, one can create a sequence of integers
3 min read
lvalue and rvalue in C language lvalue:-Â lvalue simply means an object that has an identifiable location in memory (i.e. having an address). In any assignment statement "lvalue" must have the capability to store the data.lvalue cannot be a function, expression (like a+b) or a constant (like 3 , 4 , etc.). L-value: "l-value" refer
4 min read
Increment and Decrement Operators in C The increment ( ++ ) and decrement ( -- ) operators in C are unary operators for incrementing and decrementing the numeric values by 1, respectively. They are one of the most frequently used operators in programming for looping, array traversal, pointer arithmetic, and many more.Increment Operator i
4 min read
Commonly Asked C Programming Interview Questions | Set 2 This post is second set of Commonly Asked C Programming Interview Questions | Set 1What are main characteristics of C language? C is a procedural language. The main features of C language include low-level access to memory, simple set of keywords, and clean style. These features make it suitable for
3 min read
C++ Increment and Decrement Operators Prerequisite: Operators in C++ What is a C++ increment Operator? The C++ increment operator is a unary operator. The symbol used to represent the increment operator is (++). The increment operator increases the value stored by the variable by 1. This operator is used for Numeric values only. There a
4 min read