Types of Literals in C++

Types of Literals in C++
Techiio-author
Written by Sagar RabidasFebruary 1, 2022
9 min read
C++
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Types of Literals in C++

C++ Literals

The value which is assigned to the constant variable is called literals. The constant value can be named differently as literal. For example, “const int value= 15” assigns constant integer value 15 to value and it is an integer literal. In this article, we will go through an explanation of each of the different types of literal in C++ along with its example.

Types of C++ Literals

Five different types of literal can be used in C++ as mentioned below:

  1. Integer Literal: It is used to represent integer constant.
  2. Float Literal: It is used to represent float constant.
  3. Character Literal: It is used to represent a single character.
  4. String Literal: It is used to represent the character sequence(string).
  5. Boolean Literal: It is used to represent Boolean(true or false).

Integer Literals

Integer literals in C++ represents integer constant value. Integer literals are represented mainly in two ways: prefix and suffix.

Prefix

Prefix in integer literal represents the base in which integer literal is represented.

  • Decimal-literal in base 10: This represents value in decimal (0,1,2,3,4,5,6,7,8,9) digits. The value must start with non-zero decimal digit.
Code:
// For representing decimal integer literal in base 10
const int value= 95;
const int value= 76;
const int value= 7;
  • Binary-literal in base 2: This represents value in binary (0,1) digits. The value must start with a prefix of 0b or 0B.
Code:
// For representing binary integer literal in base 2
const int value= 0b101;
const int value= 0B001;
const int value= 0B1111;
  • Octal literal in base 8: This represents value in octal (0,1,2,3,4,5,6,7) digits. The value must start with a prefix of 0.
Code:
// For representing octal integer literal in base 8
const int value= 0356;
const int value= 0732;
const int value= 0137;
  • Hex-literal in base 16: This represents value in hex (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F) digits. The value must start with a prefix of 0x or 0X.
Code:
// For representing hex integer literal in base 16
const int value= 0xA3;
const int value= 0X38C;
const int value= 0XFFFF;

Suffix

The type in which the integer literal is stored is represented by the suffix.

blogpost
Code:
// include required header files
#include <iostream>
int main()
{
// Body of main()
const int val_decimal = 79; // assigning decimal integer
const int val_hex = 0xA; // assigning hex integer literal
const int val_binary = 0b100; // assigning binary integer literal
std::cout << "Integer literal decimal base: "<< val_decimal << "\n";
std::cout << "Integer literal hex base: "<< val_hex << "\n";
std::cout << "Integer literal binary base: "<< val_binary << "\n";
return 0;
}

Float Literals

Float literals are used to represent real numbers. There are mainly two ways to represent the float literals. It can be stored in decimal format or exponential format.

  • In decimal format, float literals are represented using decimal points and exponent parts.
  • In exponential format, float literals are represented using integer and fractional parts.

Syntax:

// For representing float literals using decimal format and exponential format
const float float_leteral1 = 34.876
const float float_leteral1 = 34876E-3
Code:
// include required header files
#include <iostream>
int main()
{   // Body of main()
// float literal
const float float_leteral1 = 34.876;
const float float_leteral2 = 34876E-3;
// both float_leteral1 and float_leteral2 are same only representation is different
std::cout << "Float literal 1 value is : "<< float_leteral1 << "\n";
std::cout << "Float literal 2 value is : "<< float_leteral2 << "\n";
return 0;
}

Character Literals

Character literals are used to store a single character. A single character is represented with a single quote. A wide-character literal is represented with wchar_t as mentioned in the below example. To assign the value of wide-character literal L is used before a single character.

Syntax:

// For representing character literal
const char character_literal = 'A';
//For representing wide character literal
const wchar_t character_literal = L'A';

Code:

// include required header files
#include "iostream"
int main()
{
// Body of main()
// character literal
const char char_literal = 'H'; //’H’ is assigned to char_literal
std::cout << "Character literal value is : "<< char_literal << "\n";
return 0;
}

String Literals

String literals are used to represent string (sequence of characters). The string is represented with double quotes.

Syntax:

// For representing string literal
const char string_literal[] = "mystring";

Code:

// include required header files
#include "iostream"
int main()
{
// Body of main()
// string literal
const char string_literal[] = "mystring"; //”mystring” is assigned to string_literal
std::cout << "String literal value is : "<< string_literal << "\n";
return 0;
}

Boolean Literals

Boolean literal represents Boolean constant value. This literal type can take only two Boolean values(true/false).

Syntax:

// For representing boolean literal
const bool bool_literal1 = true;
Const bool bool_literal2 = false;
Code:
// include required header files
#include "iostream"
using namespace std;
int main()
{
// Body of main()
// assigning Boolean values to Boolean literals bool_literal1 and bool_literal2
const bool bool_literal1 = true; // assigning true to Boolean literal bool_literal1
const bool bool_literal2 = false; // assigning fasle to Boolean literal bool_literal2
cout << "Boolean literal 1 is : "<< bool_literal1 << "\n";
cout << "Boolean literal 2 is : "<< bool_literal2 << "\n";
return 0;
}

C
C++
Literals in C++
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance