EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

C++ Literals

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » C++ Literals

c++literals

Introduction to 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 literals in C++ along with its example.

Types of C++ Literals

There are five different types of literals that can be used in C++ as mentioned below:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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

Each of the above mentioned different types of literals is explained below along with its example code:

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

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

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,899 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

Code:

// For representing decimal integer literal in base 10
const int value= 95;
const int value= 76;
const int value= 7;

2. 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;

3. 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;

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

Suffix Type
u or U unsigned int
l or L long int
ul or UL unsigned long int
ll or LL long long int
ull or ULL unsigned long long int
No suffix int

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;
}

Output:

Suffix

2. 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;
}

Output:

Float

3. Character Literals

Character literals are used to store a single character. A single character is represented with single quotes. 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;
}

Output:

C++ Literals - 3

4. 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;
}

Output:

C++ Literals - 4

5. 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;
}

Output:

C++ Literals - 5

Conclusion

In this tutorial, different types of C++ literals are explained along with example code. This tutorial will be helpful while working with literals in C++.

Recommended Articles

This is a guide to C++ Literals. Here we discuss five different types of literals in C++ along with examples and appropriate syntax. You can also go through our other related articles to learn more –

  1. strcat() in C++
  2. Arithmetic Operators in C++
  3. Storage Class in C++
  4. C Literals

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More