0% found this document useful (0 votes)
18 views22 pages

Char Casting

The document covers the char data type in programming, explaining its representation of single characters using ASCII and the concept of character arithmetic. It discusses escape sequences in C++ for special characters and type casting among built-in types. Examples illustrate character manipulation and conversions, emphasizing the importance of understanding character codes and their operations.

Uploaded by

efe.bastug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views22 pages

Char Casting

The document covers the char data type in programming, explaining its representation of single characters using ASCII and the concept of character arithmetic. It discusses escape sequences in C++ for special characters and type casting among built-in types. Examples illustrate character manipulation and conversions, emphasizing the importance of understanding character codes and their operations.

Uploaded by

efe.bastug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CS201

Programming Fundamentals

Char Data Type


Outline

• Char data type


• Escape Sequences
• Type casting

Text book 9.1, 6.3.6


Char Data Type
● Is a built-in data type to represent a single character from
computer's character set
● Most commonly used standard character set is ASCII
● Letters, digits, symbols, punctuation marks, control characters
(e.g. end of line, end of file, etc.)
● Each character in ASCII set has a numeric code (0 .. 255)
● See Table F.3 in page 763 for ASCII table

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

• First 32 characters ( 0 .. 31) are non-printable control characters such as eof

• Blank character has the code 32

• Uppercase letters are consecutive and ordered in ASCII set


Code for 'A' is 65, 'B' is 66, 'C' is 67, … 'Z' is 90

• Similarly, lowercase letters are also ordered and consecutive


Code for 'a' is 97, … 'z' is 122

• Similarly, digit characters are also ordered and consecutive


Code for '0' is 48, '1' is 49, … '9' is 57

• Do not memorize the codes for letters and digits;


you can do character arithmetic
1
Why ASCII has 128 characters?
● Historical Design: ASCII (American Standard Code for Information
Interchange) was developed in the early 1960s to represent English text in
computers and communication systems which uses 7 bits in that time.
ASCII was designed for simplicity and efficiency in its era. 8 bits (1 byte)
became the standard unit of storage later, leading to the creation of
extended ASCII.

● 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

● If you apply an arithmetic operator (+ - * / %) to a character,


integer code of the character is processed
● This is an implicit type casting (conversion) similar to bool

● And if you process the result of such an operation as character,


a reverse conversion is automatically performed
● You can also process the result as integer
(actually this is the default behavior)

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

● Example: What is the value of 'Z' - 'A’ ?


• 25
(you do not have to know the codes of A and Z

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

● Example: What is the value of 'Z' - 'A’ ?


• 25
(you do not have to know the codes of A and Z

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?

digitnum = digitch - '0';

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.

char toUpper(char ch) {

if (ch >= 'a' && ch <= 'z') // if lowercase


{
return ch + ('A' - 'a'); // return its uppercase
}
return ch; // otherwise return parameter unchanged
}

2
Escape Sequences in C++
● Special symbols for some characters (mostly for control
characters) to be used in character and string literals
only

● Not to be used while entering input


● Full list is in Table 4.1 (page 103) or Table A.5 (page
716)

2
Escape Sequences in C++
● Some escape sequences
\n newline character
\t tab (used for aligned output)
\a bell
\" double quote
\' single quote
\\ backslash

● Example follows. What is the output?


cout << "\"\\\n\"\"\n\\";

"\
""
\
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

"bla bla \n bla"


'\n'

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

You might also like