# How C++ identify the base of a number?

## C++

Popular Google Pages:

This article is regarding **How C++ identify the base of a number? Integer Literals in C++.**

Last updated on: 06th December 2016.

◕ What is Integer Literals in C++?

In other word we can say **integer literal** is a constant number, such as 1, 2, 5, 100, 105 etc.

◕ C++ allows us to write integers in three different **number bases:**

1. Base 10 ( Decimal )

2. Base 8 ( Octal )

3. Base 16 ( Hexadecimal)

◕ How C++ identify the **Base of a Number?**

- C++ uses the first one digit or first two digits to identify the base of a number.

- If the **first digit** is in the range 1–9, then the base of the number is 10 i.e. it is a **Decimal Number.**

Example: 45 is a Decimal Number and its base is 10.

- If the **first digit** is 0 and the **second digit** is in the range 1–7, then the base of a number is 8 i.e. it is an **Octal Number.**

Example: 012 is an Octal Number and it's base is 8.

- If the **first two digits** are 0x ( Zero & x ) or 0X, then the base of the number is 16 i.e it is a **Hexadecimal Number.**

Example: 0x12 is a Hexadecimal Number and it's base is 16.

◕ By default, in C++, **cout** displays integers in **Decimal form,** regardless of how they are written in a program. This means, if we write a program in **Octal** or in **Hexadecimal** form
then the output will show in **Decimal** by-default.

The example is given bellow.

Let we have a program like this:

#include <iostream>

using namespace std;

int main(){

int wing = 999; // this is in Decimal form

int bird = 025; // this is in Octal form

int tree = 0X55; // this is in Hexadecimal form

cout << "Decimal value of 'wing' " << wing << endl;

cout << "By-default Octal value will show in Decimal. Here it is: " << bird << endl;

cout << "By-default Hexadecimal value will show in Decimal. Here it is: " << tree << endl;

return 0;

}

The output is:

Decimal value of 'wing' 999

By-default Octal value will show in Decimal. Here it is: 21

By-default Hexadecimal value will show in Decimal. Here it is: 85

Related article:

How to display a value in Hexadecimal form in C++?

How to display a value in Octal form in C++?

How C++ identify the base of a number?

Top of the page

# Amazon & Flipkart Special Products

Top of the page