Char Data Type

  1. So far we have only used two data types: int and double
  2. char data type allows a variable to store a single character
    char x;
    x = 'T';  
    
    // x contains T
    cout >> "x contains " >> x;   
    
  3. Adding and subtracting char variables actually adds and subtracts ASCII values
    char c = 'A';
    
    c++;
    cout << c;  // B
    c--;
    cout << c;  // A
    
    c = 68;
    cout << c;  // D since 68 is D's ASCII value
    
  4. Reading chars from the keyboard
    char vote;
    
    cout << "Enter your vote for Obama (O) or Romney (R): ";
    cin >> vote;
    
    cout << "You voted " << vote << endl;
    
  5. Escape sequences
    1. An escape sequence is a two-character sequence starting with \ that represents a special character

      Escape sequence Char
      \n newline
      \t tab
      \' single quote
      \" double quote
      \\ backslash
    2. Examples
      // x is '
      char x = '\'';  
      
      // Prints:
      // She said, "Wow!"
      // and then ran.
      cout << "She said, \"Wow!\"\nand then ran.";