Integer literal in C/C++ (Prefixes and Suffixes) - GeeksforGeeks (2024)

Improve

Improve

Improve

Like Article

Like

Save Article

Save

Report issue

Report

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.,

  1. Prefixes which indicates the base. For example, 0x10 indicates the value 16 in hexadecimal having prefix 0x.
  2. 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.
    1. 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.
    2. 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.
    3. 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.
    4. 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.
    1. int:- No suffix are required because integer constant are by default assigned as int data type.
    2. unsigned int: character u or U at the end of integer constant.
    3. long int: character l or L at the end of integer constant.
    4. unsigned long int: character ul or UL at the end of integer constant.
    5. long long int: character ll or LL at the end of integer constant.
    6. unsigned long long int: character ull or ULL at the end of integer constant.

// 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:21313985065123456789012345678912345678901234567890123456789012345678901221300

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.

// 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:12345678901245123456789012451421221300

Reference:- https://en.wikipedia.org/wiki/Integer_literal


Last Updated : 19 Jun, 2017

Like Article

Save Article

Previous

What happens when more restrictive access is given to a derived class method in C++?

Next

Coroutines in C/C++

Share your thoughts in the comments

Please Login to comment...

Integer literal in C/C++ (Prefixes and Suffixes) - GeeksforGeeks (2024)

FAQs

Integer literal in C/C++ (Prefixes and Suffixes) - GeeksforGeeks? ›

Integer literals begin with a digit and have no fractional parts or exponents. You can specify integer literals in decimal, binary, octal, or hexadecimal form. You can optionally specify an integer literal as unsigned, and as a long or long long type, by using a suffix.

What are the integer literal in C++? ›

Integer literals begin with a digit and have no fractional parts or exponents. You can specify integer literals in decimal, binary, octal, or hexadecimal form. You can optionally specify an integer literal as unsigned, and as a long or long long type, by using a suffix.

What is the integer literal of 017? ›

017 is 15 because it's assumed octal base.

What is prefix and suffix in C? ›

Prefixes − Prefixes denotes the base of the value. For example, 0x10 indicates hexadecimal value with 0x. Suffixes − Suffixes denotes the type of the value. For example, 8465484156155LL denotes a long long integer.

Which number system is the default in C for integer literals? ›

B) Suffixes: The Prefix of the integer literal indicates the type in which it is to be read. 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.

What is an example of an integer literal? ›

Integral Literals

They can have a negative (-) or a positive (+), but non-digit characters or commas aren't allowed between characters. Example: 2022, +42, -68. Octal Integer: Octal integers use a base eight and digits ranging from 0 to 7. Octal integers always begin with a “0.” Example: 007, 0295.

What are integer literals in C example? ›

Let's look at a simple example of integer literal.
  • #include <stdio.h>
  • int main()
  • {
  • const int a=23; // constant integer literal.
  • printf("Integer literal : %d", a);
  • return 0;
  • }

How do you declare an integer literal? ›

An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal. An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively.

What is the value of 013 integer literal? ›

It is extremely easy to inadvertently create an integer object with the wrong value, because '013' means 'decimal 11', not 'decimal 13', to the Python language itself, which is not the meaning that most humans would assign to this literal.

What is the difference between integer and integer literal? ›

Integer literals are represented internally as an integer, smallint, or bigint depending on the value of the literal. A literal in the range -32,768 to +32,767 is represented as a smallint. A literal in the range ‑2,147,483,648 to +2,147,483,647 but outside the range of a smallint is represented as an integer.

What are the 10 examples of prefix and suffix? ›

List of Words with a Prefix and a Suffix
PrefixBase WordSuffix
Im-Mortal-ity
Pre-Arrange-ment
Pre-Historic-al
NonBiodegrade-able
24 more rows

What is an example of a prefix and suffix in C++? ›

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.

How to find the prefix 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.

What are the three types of integer literals in C? ›

An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.

What are the three types of integral literals in C? ›

Integer Literal
  • Decimal-literal in base 10: This represents value in decimal (i.e. 0,1,2,3,4,5,6,7,8,9) digits. ...
  • Binary-literal in base 2: This represents value in binary (i.e. 0,1) digits. ...
  • Octal literal in base 8: This represents value in octal (i.e. 0,1,2,3,4,5,6,7) digits.
Apr 4, 2024

What are the types of literals in C++? ›

There are three types of integer literals used in C++ programming i.e. decimal (base 10), octal (base 8) and hexadecimal (base 16). Please note that octal literal starts with a 0 while hexadecimal literal starts with a 0x. For e.g. Decimal literal : -1, 0, 100 etc.

What are the types of integer literals? ›

1) Decimal integer literal (base 10). 2) Octal integer literal (base 8). 3) Hexadecimal integer literal (base 16, the letters 'a' through 'f' represent values (decimal) 10 through 15).

What are the three types of integer literals? ›

An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.

What is integer literal value? ›

Integer literals are represented internally as an integer, smallint, or bigint depending on the value of the literal. A literal in the range -32,768 to +32,767 is represented as a smallint. A literal in the range ‑2,147,483,648 to +2,147,483,647 but outside the range of a smallint is represented as an integer.

Top Articles
Latest Posts
Article information

Author: Nathanial Hackett

Last Updated:

Views: 5915

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Nathanial Hackett

Birthday: 1997-10-09

Address: Apt. 935 264 Abshire Canyon, South Nerissachester, NM 01800

Phone: +9752624861224

Job: Forward Technology Assistant

Hobby: Listening to music, Shopping, Vacation, Baton twirling, Flower arranging, Blacksmithing, Do it yourself

Introduction: My name is Nathanial Hackett, I am a lovely, curious, smiling, lively, thoughtful, courageous, lively person who loves writing and wants to share my knowledge and understanding with you.