Cpp - Data Type Integral Literals

Introduction

Type
Decimal
Octal
Hexadecimal
int
16
020
0x10
int
255
0377
OXff
int
32767
077777
0x7FFF
unsigned int
32768U
0100000U
0x8000U
int (32 bit-)
long (16 bit-CPU)
100000

0303240

0x186A0

long
10L
012L
0xAL
unsigned long
27UL
033UL
0x1bUL
unsigned long
2147483648
020000000000
0x80000000

The following code shows how to display hexadecimal integer literals and decimal integer literals.

Demo

#include <iostream> 
using namespace std; 

int main() /*from w w  w.  java  2 s  .  c o m*/
{ 
  // cout outputs integers as decimal integers: 
  cout << "Value of 0xFF = " << 0xFF << " decimal" 
       << endl;                 // Output: 255 decimal 

  // The manipulator hex changes output to hexadecimal 
  // format (dec changes to decimal format): 
  cout << "Value of 27 = " << hex << 27 <<" hexadecimal" 
       << endl;              // Output: 1b hexadecimal 
  return 0; 
}

Result

Integral Constants

Integral numerical constants can be represented as simple decimal numbers, octals, or hexadecimals:

  • a decimal constant (base 10) begins with a decimal number other than zero, such as 109 or 987650
  • an octal constant (base 8) begins with a leading 0, for example 077 or 01234567
  • a hexadecimal constant (base 16) begins with the character pair 0x or 0X, for example 0x2A0 or 0X4b1C. Hexadecimal numbers can be capitalized or non-capitalized.

You can designate the type of a constant by adding the letter L or l (for long), or U or u (for unsigned). For example,

Value Meaning
12L 12l correspond to the type long
12U 12u correspond to the type unsigned int
12UL 12ul correspond to the type unsigned long

Related Topic