
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are C++ Character Constants
Character constants are one or more members of the “source character set,” the character set in which a program is written, surrounded by single quotation marks ('). They are used to represent characters in the “execution character set,” the character set on the machine where the program executes. These are sometimes also called character literals.
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.
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