Char Casting
Char Casting
Programming Fundamentals
1
Char Data Type ASCII Table
1
Char Data Type ASCII Table
• Each character occupies one byte. That is why character codes are between 0 and 255
● These 128 characters include: Control characters (0–31 and 127): For
controlling hardware (e.g., newline, tab). Printable characters (32–126):
Letters, digits, punctuation, and symbols.
● Extended ASCII (256 characters) (0–255), it is not the same as the original
ASCII. The limitation of 128 characters in standard ASCII was a deliberate
choice based on the needs and technical limitations of its time.
1
Char Data Type
● Character variables are defined using the type identifier char
char a, ch, letter;
● Character literals are represented within single quotes
'A' '3' '.' 'f'
● Pay attention to difference between strings with single letter and
chars
● "A" versus 'A'
● First one is a string literal, second one is a character literal
● String variables (objects) has several private data members even if it
has a single character in it; however, char variables occupy just one
byte
1
Character Arithmetic
● If you compare two characters (or a character with an integer)
character codes are used in comparison
2
Character Arithmetic
● Example: The value of 'A' + 2 is
• 'C' if you process as a char
(you do not have to know the codes in order to reach this
result)
• 67 if you process as integer or if you do not mean any
type
2
Character Arithmetic
● Example: The value of 'A' + 2 is
• 'C' if you process as a char
(you do not have to know the codes in order to reach this
result)
• 67 if you process as integer or if you do not mean any
type
2
Character Arithmetic - Example
● Suppose digitch is a char variable and its content is a digit
character (between '0' and '9'). How can you obtain that digit
character's numeric equivalent in an int variable digitnum?
2
Character Arithmetic - Example
● Suppose digitch is a char variable and its content is a digit
character (between '0' and '9'). How can you obtain that digit
character's numeric equivalent in an int variable digitnum?
2
Character Arithmetic - Example
● Write a function that takes a character parameter and returns
the uppercase equivalent of it if parameter is a lowercase letter.
If parameter is not a lowercase letter, function returns it
unchanged.
2
Character Arithmetic - Example
● Write a function that takes a character parameter and returns
the uppercase equivalent of it if parameter is a lowercase letter.
If parameter is not a lowercase letter, function returns it
unchanged.
2
Escape Sequences in C++
● Special symbols for some characters (mostly for control
characters) to be used in character and string literals
only
2
Escape Sequences in C++
● Some escape sequences
\n newline character
\t tab (used for aligned output)
\a bell
\" double quote
\' single quote
\\ backslash
"\
""
\
2
Escape Sequences in C++
● If you want to represent a non-printable character (such as new
line) in a string or character literal, you have to use non-
printable escape sequences
2
Escape Sequences in C++
● For printable ones, you may use escape sequences whenever needed
● You may or may not use escape sequence to represent a double
quote character in a char literal
● That means '\"' and '"' are the same
● You may or may not use escape sequence to represent a single
quote character in a string literal
● That means "bla \' bla" and "bla ' bla" are the same
● However, you have to represent a double quote in a string literal as
an escape sequence; similarly, you have to represent a single quote
in a char literal as an escape sequence
● To represent a backslash, you always have to use escape sequence
in both char and string literals
2
Type Casting
● Conversion of an expression to another type
● Among built-in types like double, int, char, bool
● String cannot be used in type casting
● Syntax
type_name (expression)
● First expression is evaluated, then conversion is done
● Examples
cout << double (3) / 4;
● Output is 0.75
● First 3 is converted into double (3.0), then divided by 4
cout << 3 / 4;
● Without casting, integer division, output is 0
cout << double (3 / 4);
● First 3 / 4 is evaluated to 0, then result is converted
● Output is 0 2
Type Casting Examples
● Real division using two integers
int a, b;
double div;
...
div = double (a) / b;
● Conversion from double to int
cout << int (3.9);
● Double value is truncated
● Output is 3
● Conversion from signed to unsigned integers
cout << unsigned int (-100);
● Output is NOT 100, output is 4294967196
● The bit sequence to represent –100 is the same as the one of 4294967196
2
Type Casting Examples
● Conversion from int to char
int i = 72;
cout << char (i);
● Output is H
● It is the character with code 72
● Complex example: what is the value of the following expression?
3 + double (62 * (int ('C') - int ('A'))) / 5
124
124.0
24.8
27.8 2