CPP Characters and Integers

Characters and Integers

  • The primitive type char is an integer with a maximum value of 255 occupying a byte
  • As this is true any integer type variable can be casted to a char type using only the lowest part of the integer as the character:
    Source code Result
    // Printing Characters Based on Numbers.
    #include <iostream>
    int main(){
      for (int i = 32; i<128; i++)
        // casting int i to a char type
        std::cout << (char) i;
      return 0;
    }
     !"#$%&'()*+,-
    ./0123456789:;<=>?@
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    [\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    

    You can download this example here (needed tools can be found in the right menu on this page).

  • Some special character can be printed using an escape notation (\):
    Escape code description
    \a Bell (alert)
    \b Backspace
    \f Form feed
    \n New line
    \r Carrige return
    \t Tab
    \v Vertical tab
    \" double quote
    \' Single quote
    \? Question mark
    \\' Backslash
    \000' Octal notation
    \xhhh' Hexadecimal notation
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.