Characters
Character Data char data type – Represents one character – char literals indicated with ' '
Char vs String Char – One character – literals use ' ' String – 0+ characters – literals use " "
Character Data char is integer data type – 1 byte of space : 256 values – Assumed to represent characters
Chars as Integers Integers understood as ASCII codes – Can store integers as chars
Chars And Math Doing math – char converted to an int:
Chars And Math Adding/subtracting and cast back to character shifts char:
Letter Number Identify letter of alphabet: 'f' – 'a' –
Random Char Generate random char: – Make number 0-25 – Add to 'a'
Comparisons Chars compare like other numbers:
But Capitals different than lower case: – lower case is "greater"
Why is this broken? This prints nothing:
Why is this broken? This prints nothing: '3' is char 51 on ASCII chart if( 51 == 3 )
Fixed This works:
Special Chars Some characters hard to represent – return, tab, ' Escape sequences used to represent them – \ means next char is special: '\t' : tab char '\'' : ' char
Escape Codes Common codes:
Escape Codes Used in strings and as chars:
Case Conversion Convert lower to upper: – Subtract 'a' to find letter number – Add that amount to 'A'
Char Functions library has char related functions: – – all return int is… interpret as boolean to… interpret as char
Conversion 2 More readable conversion:
Conversion 3 toupper / tolower do their own sanity checks