02 C Examples
02 C Examples
C Programming Examples!
Why?"
The fundamentals of C provide a foundation for the
systematic coverage of C that will follow"
A power programmer knows the fundamentals of C well"
DFA are useful in many contexts "
A very important context: Assignment 1"
How?"
1
C programming examples"
Echo input to output"
Convert all lowercase letters to uppercase"
Convert first letter of each word to uppercase"
int main(void) {
int c;
c = getchar();
putchar(c);
return 0;
Why return a
value?"
#include <stdio.h>
int main(void) {
int c, i;
#include <stdio.h>
int main(void) {
int c;
for ( ; ; ) {
c = getchar();
putchar(c);
}
return 0;
return 0;
#include <stdio.h>
int main(void) {
int c;
for ( ; ; ) {
c = getchar();
if (c == EOF)
break;
putchar(c);
}
return 0;
}
" putchar(c);
while ((c=getchar())!=EOF)
do some stuff
putchar(c);"
done yet?
do more stuff
after the loop
9
for (;;) {
c = getchar();
if (c == EOF)!
break;
!putchar(c);
}!
c = getchar();
while (c!= EOF){
! putchar(c);
c = getchar();
}
10
Character I/O"
Which approach
is best?"
Including stdio.h
Functions getchar() and putchar()
Representation of a character as an integer"
Predefined constant EOF
Program design:"
repeat in a loop
Read a character
If unsuccessful, break out of loop
If the character is lower-case, convert to uppercase
Write the character
Operators"
"
"
11
12
ASCII!
Implementation in C!
10
11
12
13
14
15
HT
LF
VT
FF
CR
SO
SI
SUB ESC FS
GS
RS
US
"
&
'
48
<
>
64
80
96
112
DEL
#include <stdio.h>
int main(void) {
int c;
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
if ((c >= 97) && (c < 123))
c -= 32;
putchar(c);
}
return 0;
}
13
It works!!
14
Its a !
"
Submit"
B-"
15
16
Ugly. "
And works for
ASCII only"
int c;
Clean"
Readable"
Maintainable"
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
return 0;
17
#include <stdio.h>
int main(void) {
NAME
int c;
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
if ((c >=
c +=
a ) && (c <=
Better. "
But still
assumes that
alphabetic
character codes
are contiguous"
ctype(3C)
Section 3C is for C
library functions"
ctype, isdigit, isxdigit, islower, isupper, isalpha, isalnum, isspace, iscntrl, ispunct, isprint,
isgraph, isascii - character handling
z ))
a ;
putchar(c);
}
return 0;
}
18
19
SYNOPSIS
#include <ctype.h>
int isalpha(int c);
int isupper(int c);
int islower(int c);
int isdigit(int c);
int isalnum(int c);
int isspace(int c);
int ispunct(int c);
int isprint(int c);
int isgraph(int c);
int iscntrl(int c);
int toupper(int c);
int tolower(int c);
DESCRIPTION
These macros classify charactercoded integer values. Each is a
predicate returning non-zero for true, 0
for false...
The toupper() function has as a
domain a type int, the value of which is
representable as an unsigned char or
the value of EOF.... If the argument of
toupper() represents a lower-case
letter ... the result is the corresponding
upper-case letter. All other arguments
in the domain are returned unchanged.
20
#include <stdio.h>
#include <ctype.h>
int main(void) {
int c;
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
Returns non-zero"
if (islower(c))
c = toupper(c); (true) iff c is a lowercase"
character"
putchar(c);
}
return 0;
}
21
% ls
upper.c
% gcc217 upper.c o upper
% ls
upper upper.c
% upper
We ll be on time today!
WE LL BE ON TIME TODAY!
^D
%
22
Output Redirection!
23
24
Representing characters"
Manipulating characters"
Arithmetic on characters"
Functions like islower() and toupper()
States!
Where am I?"
Im inside a word"
Ive seen the first letter of it but not yet the space after it"
If I see a letter now, I should not capitalize it"
Im not inside a word "
If I see a letter now, I should capitalize it"
Im in my car"
If I get a phone call I shouldnt take it"
Im in my room"
If I get a phone call I can take it"
letter"
(print uppercase equivalent)"
What am I doing?"
1"
26
27
not-letter"
(print)"
not-letter"
(print)"
2"
letter"
(print)"
28
Implementation Skeleton!
#include <stdio.h>
#include <ctype.h>
int main (void) {
int c;
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
<process one character>
}
return 0;
}
case 1:
1"
break;
#include <stdio.h>
#include <ctype.h>
int main(void) {
int c; int state=1;
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
switch (state) {
case 1:
letter"
(print)"
30
if (isalpha(c)) {
putchar(toupper(c));
state = 2;
} else putchar(c);
break;
case 2:
case 2:
default:
2"
Complete Implementation!
break;
not-letter"
(print)"
not-letter"
(print)"
29
not-letter
letter
letter
1
2
not-letter
Implementation!
switch (state) {
Implementation Skeleton!
if (!isalpha(c)) state = 1;
putchar(c);
if input is not a letter
break;
}
}
return 0;
32
It works!!
"
Submit"
33
Your grade!
34
OK, That s a B!
"
B"
What now?"
States should have names, not just 1, 2
35
36
int main(void) {
int c; enum Statetype state = NOT_IN_WORD;
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
switch (state) {
case NOT_IN_WORD:
if (isalpha(c)) {
putchar(toupper(c));
state = IN_WORD;
} else putchar(c);
break;
case IN_WORD:
if (!isalpha(c)) state = NOT_IN_WORD;
putchar(c);
break;
37
}
}
return 0;
38
Submit"
B+"
39
40
10
Huh?!
Improvement: Modularity!
What now?"
Should handle each state in a separate function
Each state handling function does the work for a given
state, including reading the input and taking the action
It returns the new state, which we will store in the state
variable for the next iteration of our infinite loop
41
Improvement: Modularity!
#include <stdio.h>
#include <ctype.h>
enum Statetype {NOT_IN_WORD,IN_WORD};
enum Statetype handleNotInwordState(int c) {...}
enum Statetype handleInwordState(int c) {...}
int main(void) {
int c;
enum Statetype state = NORMAL;
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
switch (state) {
case NOT_IN_WORD:
state = handleNotInwordState(c);
break;
case IN_WORD:
state = handleInwordState(c);
break;
}
}
return 0;
}
42
Improvement: Modularity!
if (isalpha(c)) {
putchar(c);
putchar(toupper(c));
if (!isalpha(c))
state = IN_WORD;
state = NOT_IN_WORD;
else
else {
state = IN_WORD;
putchar(c);
return state;
state = NOT_IN_WORD;
}
return state;
}
43
44
11
Seriously??!
"
No comments"
Should add (at least) function-level comments
A-"
45
Function Comments
46
48
12
An A Effort!
An A Effort!
#include <stdio.h>
#include <ctype.h>
/*------------------------------------------------------------*/
enum Statetype {NOT_IN_WORD, IN_WORD};
/*------------------------------------------------------------*/
/* handleNormalState: Implement the NOT_IN_WORD state of the DFA.
/* c is the current DFA character. Return the next state.
*/
*/
*/
*/
/*------------------------------------------------------------*/
enum Statetype handleInwordState(int c) {
/*------------------------------------------------------------*/
putchar(c);
if (!isalpha(c))
putchar(toupper(c));
state = NOT_IN_WORD;
state = IN_WORD;
}
else
else {
state = IN_WORD;
putchar(c);
return state;
state = NOT_IN_WORD;
}
return state;
}
49
An A Effort!
/*------------------------------------------------------------*/
/* main: Read text from stdin. Convert the first character
*/
for ( ; ; ) {
c = getchar();
if (c == EOF) break;
switch (state) {
case NOT_IN_WORD:
state = handleNotInwordState(c);
break;
case IN_WORD:
state = handleInwordState(c);
break;
}
}
return 0;
}
50
51
Readable"
Meaningful names for variables and values"
qqq is not meaningful. Nor are foo and bar"
Modular"
Multiple functions, each of which does one well-defined job"
Function-level comments"
Should describe what function does"
See K&P book for style guidelines specification"
"
52
13
banano "
nnnnnnnanofff "
banananonano "
bananananashanana "
Valid numbers"
n
n
S
a
No input shown on an
arc => any other input
53
-34 "
78.1 "
+298.3 "
-34.7e-1 "
34.7E-1 "
7. "
.7 "
999.99e99 "
Invalid numbers"
abc "
-e9 "
1e "
+ "
17.9A "
0.38+ "
. "
38.38f9 "
54
Summary !
Examples illustrating C"
Overall program structure"
Control statements (if, while, for, and switch)"
Character input/output (getchar() and putchar())"
55
14