Namespaces
Variants
Views
Actions

integer literal

From cppreference.com
< cpp‎ | language
Revision as of 07:01, 3 January 2014 by Cubbi (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

Allows values of integer type to be used in expressions directly.

Contents

Syntax

An integer literal is a primary expression of the form

decimal-literal integer-suffix(optional) (1)
octal-literal integer-suffix(optional) (2)
hex-literal integer-suffix(optional) (3)
binary-literal integer-suffix(optional) (4) (since C++14)

where

  • decimal-literal is a non-zero decimal digit (1, 2, 3, 4, 5, 6, 7, 8, 9), followed by zero or more decimal digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
  • octal-literal is the digit zero (0) followed by zero or more octal digits (0, 1, 2, 3, 4, 5, 6, 7)
  • hex-literal is the character sequence 0x or the character sequence 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)
  • binary-literal is the character sequence 0b or the character sequence 0B followed by one or more binary digits (0, 1)
  • integer-suffix, if provided, may contain one or both of the following (if both are provided, they may appear in any order:
  • unsigned-suffix (the character u or the character U)
  • either the long-suffix (the character l or the character L) or the long-long-suffix (the character sequence ll or the character LL)

Explanation

1) Decimal integer literal (base 10, the first digit is the most significant)
2) Octal integer literal (base 8, the first digit is the most significant)
3) Hexadecimal integer literal (base 16, the first digit is the most significant, the letters 'a' through 'f' represent values (decimal) 10 through 15)
4) Binary integer literal (base 2, the first digit is the most significant)

The following variables are initialized to the same value:

int d = 42;
int o = 052;
int x = 0x2a;
int X = 0X2A;
int b = 0b101010;

The following variables are also initialized to the same value:

unsigned long long l1 = 18446744073709550592ull;
unsigned long long l2 = 18446744073709550592llu;
unsigned long long l3 = 18446744073709550592uLL;
unsigned long long l4 = 18446744073709550592LLU;

The type of the literal

The type of the integer literal is the first type in which the value can fit, from the list of types which depends on which numeric base and which integer-suffix was used.

Types allowed for integer literals
suffix decimal bases hexadecimal or octal bases
no suffix int

long int
long long int

int

unsigned int
long int
unsigned long int
long long int
unsigned long long int

u or U unsigned int

unsigned long int
unsigned long long int

unsigned int

unsigned long int
unsigned long long int

l or L long int (until C++11)

unsigned long int (until C++11)
long int (since C++11)
long long int (since C++11)

long int

unsigned long int
long long int
unsigned long long int

both l/L and u/U unsigned long int

unsigned long long int

unsigned long int

unsigned long long int

ll or LL long long int unsigned long int

unsigned long long int

both ll/LL and u/U unsigned long long int unsigned long long int

If the value of the integer literal is too big to fit in any of the types allowed by suffix/base combination and the compiler supports extended integer types (such as __int128) the literal may be given the extended integer type -- otherwise the program is ill-formed.

Notes

Letters in the integer literals are case-insensitive: 0xDeAdBaBeU and 0XdeadBABEu represent the same number (one exception is the long-long-suffix, which is either ll or LL, never lL or Ll)

There are no negative integer literals. Expressions such as -1 apply the unary minus operator to the value represented by the literal, which may involve implicit type conversions.

In C prior to C99 (but not in C++), unsuffixed decimal values that do not fit in long int are allowed to have the type unsigned long int.

Example

#include <iostream>
 
int main()
{
std::cout << 123    << '\n'
          << 0123   << '\n'
          << 0x123  << '\n'
          << 0b10   << '\n'
          << 12345678901234567890ull << '\n'
          << 12345678901234567890u   << '\n'; // the type is unsigned long long even
                                              // without a long long suffix
 
//   std::cout << -9223372036854775808 << '\n'; // error: the value
                // 9223372036854775808 cannot fit in signed long long, which is the
                // biggest type allowed for unsuffixed decimal integer literal
     std::cout << -9223372036854775808u << '\n'; // unary minus applied to unsigned
                // value subtracts it from 2^64, this gives 9223372036854775808
     std::cout << -9223372036854775807 - 1 << '\n'; // correct way to calculate
                                                    // the value -9223372036854775808
}

Output:

123
83
291
2
12345678901234567890
12345678901234567890
9223372036854775808
-9223372036854775808