You are on page 1of 10

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #10, August 16, 2011

Please switch off your mobile phones.

Announcements
Monday lab scheduled on 15th August will instead be held on Saturday, 20th August. Monday lab scheduled on 22nd August will instead be held on Saturday, 27th August. Wednesday lab scheduled on 31st August will instead be held on Saturday, 3rd September.

Lec-10

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Getting out of loops
break continue

Char data type


Storing a char in memory
ASCII

P i i a character Printing h Reading a character

Lec-10

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Reading a character


%c is used to indicate scanf that a single character is to be read.
scanf (%c, &c)

What if there are multiple characters to be read?

Lec-10

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Inputting Multiple Characters: Version 1


char a, b, c; printf (Type first character\n); ( Type character\n ); scanf (%c, &a); printf (Type second character\n); scanf (%c, &b); printf (Type third character\n); scanf (%c, &c); printf (%c\n%c\n%c\n, a, b, c);

Does not Work! Enter is read as a character.


4

Lec-10

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Inputting Multiple Characters: Version 2


char a, b, c; printf (Type three characters\n); scanf (%c, &a); scanf (%c, &b); scanf (%c, &c); p printf (%c\n%c\n%c\n, a, b, c); ( )

You have to type all three characters together.

Lec-10

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Inputting Multiple Characters: Version 3


char a, b, c; printf (Type first character\n); ( Type character\n ); scanf (%c, &a); printf (Type second character\n); scanf ( %c, &b); // Added a space before %c printf (Type third character\n); scanf ( %c, &c); // Added a space before %c printf (%c\n%c\n%c\n, a, b, c);

Works! We could fix the newline problem.


6

Lec-10

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Reading variable number of characters (version 4)


int i, n; char c; printf (E t the number of characters\n); i tf (Enter th b f h t \ ) scanf (%d, &n); for (i = 0; i < n; i = i + 1) { scanf (%c, &c); printf (%c\n, c); }

scanf requires Enter before it can read Typed characters are remembered as input.
Lec-10 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 7

Reading variable number of characters (version 5)


int i, n; char c; printf (E t the number of characters\n); i tf (Enter th b f h t \ ) scanf (%d, &n); for (i = 0; i < n; i = i + 1) { scanf ( %c, &c); // added a space before %c printf (%c\n, c); }

scanf requires Enter before it can read Typed characters are remembered as input.
Lec-10 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8

Arithmetic operators
+ and can be used with character variables Best to avoid multiplication, division and modulus a + 2 will become c z 2 will become x A + 1 = 65 + 49 = 114 = r (avoid) 1 * 2 = 49 * 2 = 98 = b (avoid) ( )

Lec-10

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Relational operators
All relational operators are valid with characters
a > b is false (and can be assumed in all environments) The following happened to be true in our lab and on most computers, but dont assume these, in general
a == 97 a > A 5 > 5 5

Note the automatic conversion to integers for comparison.

Lec-10

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

10

Boolean
Integers are treated as booleans p Value 0 represents false Any non-zero (or is it non-negative?) value is true Example:
(3> 5) will be printed as 0 (3 < 5) will be printed as 1

!4 means false !0 means true 4 && 0 is false 4 || 0 is true


Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 11

Lec-10

Unary operators
+ (unary plus) - (unary minus) ++ (increment by one) -- (decrement by one) = (assignment Unary operators associativity is right-to-left Post-increment (i++) Pre-increment (++i) First four unary operators have highest precedence amongst all operators. Assignment operator has lowest precedence
Lec-10 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 12

Unary Operators (Examples)


45 + -11 evaluates to 34 45 + +11 evaluates to 56 l t t i = 67; j = i++;
j becomes 67, i becomes 68

i = 72; j = --i;
j and i become 71.

c = u; ++c; u ; c;
c becomes v

x = y = 45
Both x and y become 45.
Lec-10 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 13

Operator Precedence and Associativity


Operator p !, ++, - -, unary +, unary *, /, % +, <, <=, >, >= ==, != && || = Lowest Precedence Highest Associativity y Right-to-left Left-to-right Left-to-right Left-to-right Left-to-right Left-to-right Left-to-right Right-to-left

Lec-10

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

14

Single character input


getchar () reads a single character Returns an integer value, should be converted to a char Example: Counting number of characters in input and rewriting the line:
char c; int i = 0; c = (char) getchar(); while (c != EOF) // Stop input with Ctrl-D { printf (%c\n, c); i++; c = (char) getchar(); } printf (Number of characters input is %d\n, i);
Lec-10 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 15

Single Character Input


Number of characters read in the previous program include Enter EOF is a special value to indicate end of input.

Lec-10

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

16

Undoing the reading of a character


ungetc is a standard C library function. function ungetc (int c, FILE *stream) Typical usage
ungetc ((int) c, stdin); The character referred to by variable c is being placed in the input stream. I useful, when you want to first know what is the input Is f l h fi k h i h i character, before doing the full fledged reading of the input.

Lec-10

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

17

Any Questions?

Lec-10

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

18

10

You might also like