
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What are Character Literals in C++?
A character literal is a type of literal in programming for the representation of a single character's value within the source code of a computer program.
In C++, A character literal is composed of a constant character. It is represented by the character surrounded by single quotation marks. There are two kinds of character literals −
- Narrow-character literals of type char, for example 'a'
- Wide-character literals of type wchar_t, for example L'a'
The character used for a character literal may be any graphic character, except for reserved characters such as newline ('\n'), backslash ('\'), single quotation mark ('), and double quotation mark ("). Reserved characters are be specified with an escape sequence. For example,
Example
#include <iostream> using namespace std; int main() { char newline = '\n'; char tab = '\t'; char backspace = '\b'; char backslash = '\'; char nullChar = '\0'; cout << "Newline character: " << newline << "ending" << endl; cout << "Tab character: " << tab << "ending" << endl; cout << "Backspace character: " << backspace << "ending" << endl; cout << "Backslash character: " << backslash << "ending" << endl; cout << "Null character: " << nullChar << "ending" << endl; }
Output
This gives the output −
Newline character: ending Tab character: ending Backspace character: ending Backslash character: \ending Null character: ending
- Related Articles
- What is the difference between character literals and string literals in Java?
- What are literals in Java?
- What are literals in C++?
- What are Literals in Python?
- What are JSP literals?
- Character constants vs String literals in C#
- What are Boolean literals in Java?
- What are Boolean Literals in C++?
- What are string literals in C#?
- What are integer literals in C#?
- What are Perl Numerical Literals?
- What are Perl String Literals?
- What are floating point literals in C#?
- What are string literals in C language?
- Type difference of character literals in C vs C++

Advertisements