0% found this document useful (0 votes)
3 views

Lecture05 - Copy

The document outlines Lecture 5 of the GNG1106 course at the University of Ottawa, focusing on the char type in C programming and decision structures like the switch statement. It explains how characters are stored, encoded using ASCII, and how to declare and manipulate char variables. Additionally, it discusses the use of switch statements for decision-making in programming, providing examples and coding demonstrations.

Uploaded by

Mo
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)
3 views

Lecture05 - Copy

The document outlines Lecture 5 of the GNG1106 course at the University of Ottawa, focusing on the char type in C programming and decision structures like the switch statement. It explains how characters are stored, encoded using ASCII, and how to declare and manipulate char variables. Additionally, it discusses the use of switch statements for decision-making in programming, providing examples and coding demonstrations.

Uploaded by

Mo
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/ 19

GNG1106

Fundamentals of Engineering Computation


Lecture 5

Instructor: Mohammad Al Ridhawi

University of Ottawa

Winter 2025
In-Class Exercise: Go To Brightspace
Outline

1 The char Type

2 More on Decision Structures


The char Type

Outline

1 The char Type

2 More on Decision Structures


The char Type

In C, a character, such as ‘a’, ‘x’, ‘?’, ‘$’, ... , is stored in a


variable of char type.
A char typed value takes one byte (i.e., 8 bits) to store.
A character is represented (or “encoded”) as an integer (which is
represented in binary) inside computers.
The most common method for encoding characters into integers
(or binary strings) is the ASCII (American Standard Code for
Information Interchange) Code.
The ASCII code represents 128 characters and special symbols
using 7 bits.
Under the ASCII encoding, each character has an integer value
and a 7-bit representation, which fits in the one-byte storage for
the char type.
The char Type

ASCII Table

Picture taken from https://simple.wikipedia.org/wiki/ASCII


The char Type

The characters in the range 1 through 31 inclusively, are special


characters.
For example, the character [BELL] (numerical value 7) can be
used to ring the bell on the computer.
The ASCII codes for characters ‘0’ through ‘9’ do not have
numerical value 0 to 9
Note that characters ‘A’ through ‘Z’ and ‘a’ through ‘’z have
ASCII codes in increasing order of their numerical values.
Lowercase letter = uppercase letter + 32
In C, the notation for a character literal requires single quotation
marks, such as, 'a', 'B', '3', '&', '$', '\n' ...
The char Type

Declaring a char-typed variable: char myChar;


Assigning values to a char-typed variable: Two ways.
myChar = 'a'; // note: single quotes, not double quotes!
myChar = 97;
Declaration with initialization:
char myChar='a';
char myChar=97;
A variable of type char can be interpreted either as a character or
as an integer.
printf("%d", myChar) //print as an integer
printf("%c", myChar) //print as a character
The char Type

As a number, a char-typed value has range from -128 to 127.


We can also use int-typed variables to store characters, but
caution that as a number, a char has smaller range than an int
int A=353; printf("%c", A);
On MacOS, this appears to print 'a' (since 353%256=97='a')
Since char-typed values are merely integers (which can be
interpreted as characters), arithmetic, equality, and relational
operations can all be applied to them (except that their range is
rather small) as they do to other fixed-point values.
expression value
'a'+'b' 195 (=97+98)
'a' == 'b' 0 (FALSE)
'a'<'b' 1 (TRUE)
The char Type

Read Characters from the Keyboard

char myChar;
scanf("%c", &myChar);

Note
Caution! scanf() is known to be fragile for reading characters.
The char Type

The Problem of scanf in Reading Characters


char myChar1, myChar2;
scanf("%c", &myChar1);
printf("myChar1 is %c\n",
myChar1);
scanf("%c", &myChar2);
printf("myChar2 is %c\n",
myChar2);

When executing this code, if you hit the ENTER key after
entering the first character for the first call of scanf, the code also
automatically executes the second scanf and the ENTER key (the
character '\n') is read and loaded into variable myChar2.
It is recommended that a line “fflush(stdin);” be added before
every call of scanf. But depending on the OS and compiler, this
may or may not solve the issue.
Later we will introduce better ways to read characters.
The char Type

Coding Demonstration
More on Decision Structures

Outline

1 The char Type

2 More on Decision Structures


More on Decision Structures

switch Statement

switch (I)
{
case CONST1: switch, case, default, and break
DoA; are all keywords.
break; I is an expression that evaluates to an
case CONST2: integer.
DoB; CONST1, CONST2 ... are integer literals.
break; DoA, DoB, ... DoX are each a line of
... code or a block of code.
default: If any of them is a block of code, the
DoX; block needs not to be enclosed by
} curly brackets { }.
Arbitrarily many case’s are allowed.

Instructor: Professor Yongyi Mao (Universit


GNG1106 y ofFOtta
undamen
wa) tals of Engineering Computation
Fall 2024
Lecture
More on Decision Structures

The program compares the value


of I against the integer value
listed in each case in order.
Yes
I=CONST1? Execute DoA; If the value I equals to a listed
No
value, the program exits the
switch statement; otherwise, it
Yes
I=CONST2? Execute DoB; checks the next case.
No If no match is found in all cases,
the program executes the code
listed under default and then
Execute DoX; exits the switch statement.
When the program exits the
switch statement, it continues
with the next line right after the
switch statement.
More on Decision Structures

Note
To implement the desired logic of the switch statement, the break
statements must be included. For historical reasons, if the break
statements are not included, the compiler will consider the code
syntactically correct. In this case, the code will have a strange
behaviour: the code under each case after the true case will be
executed.

Highlight
The switch statement can only be used to compare integer expression
against integer values!

Note
If needed, one can nest if/if-else statement inside a switch statement,
or vice versa.
More on Decision Structures

#include <stdio.h>
int main()
{
int x;
printf("enter 1, 2, or 3\n");
scanf("%d", &x);
switch (x)
{
case 1:
printf("You selected option 1\n");
break;
case 2:
printf("You selected option 2\n");
break;
case 3:
printf("You selected option 3\n");
break;
default:
printf("Invalid selection!\n");
}
return 0;
}
More on Decision Structures

Write a “City Hall” program

Write a program to simulate a “kiosk” at the “City Hall”. The program


asks the user to enter a number to indicate his desired service, and
prints an instruction. For example. if the user wants to renew his
license, he enters 1 and the program prints a message telling him to go
to Counter 5.
User Selection Service Option Instruction
1 Renew License Go to Counter 5
2 Renew Sticker Go to Counter 8
3 Pay for Parking Ticket Go to Counter 2
Use symbolic constants!
More on Decision Structures

Coding Demonstration

You might also like