• Home
  • 1.4 Character Datatype Handling – Part 1

1.4 Character Datatype Handling – Part 1

View Categories

1.4 Character Datatype Handling – Part 1

< 1 min read

Table of Contents

The char data type stores a single character value. Characters in C++ are enclosed in single quotes and are internally represented using their corresponding integer character codes.

This example demonstrates how a character is stored in a char variable and displayed using the standard output stream.

Source Code #

#include <iostream>

int main()
{
    char grade = 'A';

    std::cout << "Grade: " << grade << '\n';

    return 0;
}

Output #

Grade: A

Explanation #

  • char grade = 'A'; declares a character variable and initializes it with the character 'A'.
  • Character literals are enclosed in single quotes (' '), distinguishing them from string literals, which use double quotes (" ").
  • A char variable stores only one character at a time.
  • Internally, the character 'A' is stored as its corresponding character code. The output stream automatically displays it as the character rather than its numeric value.
  • std::cout << "Grade: " << grade << '\n'; prints the text followed by the character stored in the variable.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *