4.4.1: Literals and Constants - Integers
- Page ID
- 29032
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)
Integer Literals
These are used to represent and store the integer values. Integer literals are expressed in two types i.e.,
- Prefixes: The Prefix of the integer literal indicates the base in which it is to be read.
For example:0x10 = 16 Because 0x prefix represents a HexaDecimal base. So 10 in HexaDecimal is 16 in Decimal. Hence the value 16.
There 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 0 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
- 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).
- Suffixes: The Prefix of the integer literal indicates the type in which it is to be read.
For example:
12345678901234LL indicates a long long integer value 12345678901234 because of the suffix LL
These are represented in many ways according to their data types.
- int: No suffix is required because integer constant is by default assigned as an int data type.
- unsigned int: character u or U at the end of an integer constant.
- long int: character l or L at the end of an integer constant.
- unsigned long int: character ul or UL at the end of an integer constant.
- long long int: character ll or LL at the end of an integer constant.
- unsigned long long int: character ull or ULL at the end of integer constant.
Example:
#include <iostream>
using namespace std;
int main()
{ // constant integer literal
const int intVal = 10;
cout << "Integer Literal: " << intVal << "\n";
return 0;
}
Integer Literal: 10
Adapted from:
"Types of Literals in C/C++ with Examples" by Chinmoy Lenka, Geeks for Geeks