C++ - Characters & char

Overview

Estimated time: 15–20 minutes

Work with characters using the char type, escapes, and classification functions.

Learning Objectives

  • Declare char literals and understand signedness portability.
  • Classify characters with std::isalpha, std::isdigit, etc.

Prerequisites

Examples

#include <cctype>
#include <iostream>
int main(){
  char ch = '\n';
  unsigned char uch = static_cast('\xFF');
  std::cout << std::isalpha('A') << std::isdigit('7');
}

Common Pitfalls

  • Pass unsigned char to functions to avoid UB when char is signed and value is negative.