C++ Character Data Type

In C++, the char data type is used to store a single character. This could be a letter, number, or symbol, and it must be enclosed in single quotes (' ').

Tutorials dojo strip



Declaring a Character Variable

To define a character variable, use the char keyword and assign it a single character.

Example

#include <iostream>
using namespace std;

int main() {
    char bikeModel = 'R';
    cout << "Bike model code: " << bikeModel;
    return 0;
}




Using ASCII Values in char

Characters can also be represented using ASCII codes, which are numeric values corresponding to different characters.

Example

#include <iostream>
using namespace std;

int main() {
    char brandA = 72, brandB = 79, brandC = 78;
    
    cout << "Motorcycle brand initials: ";
    cout << brandA;  // Outputs 'H'
    cout << brandB;  // Outputs 'O'
    cout << brandC;  // Outputs 'N'

    return 0;
}




Key Points About char in C++

  • A single character is enclosed in single quotes ('A').
  • char only stores one character, unlike string, which handles multiple.
  • ASCII values can be used to represent characters numerically.
Tutorials dojo strip
Scroll to Top