Title: Programming Language ? C Tutorial Introduction
1Programming Language ? CTutorial Introduction
2Content
- Background
- Getting Started
- IDE (Integrated Development Environment)
- Main Parts of C Programs
- Variables and Arithmetic Expressions
- The for statement
- Symbolic Constants
- Character Input and Output
- Arrays
- Functions
- Character Arrays
- External Variables and Scope
3Programming Language ? CTutorial Introduction
4Textbooks
- C How to Program, 5th Edition, by Harvey M.
Deitel , Paul J. Deitel. - The C programming Language, 2nd Editon, by Brian
W. Kernighan and Dennis M. Ritchie, Prentice-Hall
in 1988.
5Grading
- Programming 50
- Midterm 25
- Final 25
6The C Language
- Currently, the most commonly-used language for
embedded systems - High-level assembly
- Very portable
- compilers exist for virtually every processor
- Easy-to-understand compilation
- Produces efficient code
- Fairly concise
7C History
The Development of the C Language
- Developed between 1969 and 1973 along with Unix
- Due mostly to Dennis Ritchie
- Designed for systems programming
- Operating systems
- Utility programs
- Compilers
- Filters
- Evolved from B, which evolved from BCPL
8Computer Architecture
9Programming Language ? CTutorial Introduction
10Setting Program Development Environment
11Setting Program Development Environment
12Setting Program Development Environment
13Setting Program Development Environment
14Setting Program Development Environment
15Setting Program Development Environment
SystemRoot\system32\cmd.exe /K
"F\VS6\VC98\Bin\VCVARS32.BAT"
16Setting Program Development Environment
17Our First C Program ? Hello World
18Our First C Program ? Hello World
include ltstdio.hgt main() printf("hello,
world\n")
19Our First C Program ? Hello World
include information about standard library
include ltstdio.hgt main() printf("hello,
world\n")
define a function called main that received no
argument values
20Our First C Program ? Hello World
include ltstdio.hgt main() printf("hello,
world\n")
define a function called main that received no
argument values
21Our First C Program ? Hello World
include ltstdio.hgt main() printf("hello,
world\n")
statements of main are enclosed in braces
22Our First C Program ? Hello World
include ltstdio.hgt main() printf("hello,
world\n")
main calls library function printf
23Our First C Program ? Hello World
24Compiling and Linking
25Compiling and Linking
26Compiling and Linking
27Compiling and Linking
28Compiling and Linking
29Compiling and Linking
30Execution
31Execution
32Exercises
- Create some typo errors for Hello.c, and redo the
compiling/linking process. Watch and understand
the screen output reported by cl. - Add more printf statements in Hello.c and redo
the compiling/linking/execution process.
33Programming Language ? CTutorial Introduction
34Visual C 6.0
35Visual C 6.0
36Visual C 6.0
37Win32 Console Application
38Win32 Console Application
39Win32 Console Application
40Hello_IDE
41Hello_IDE
42Hello_IDE
43Hello_IDE
44Hello_IDE
45Hello_IDE
46Hello.c
47Building Application
48Building Application
49Execution
50Execution
51Files Generated by IDE
52Programming Language ? CTutorial Introduction
53Main Parts
- Headers/Comments/Expressions
include ltstdio.hgt / Our first C Program
/ main() printf("hello, world\n")
54Example Identify the three parts in the following
program
55Remark
C also uses // to comment a single line.
56The Syntax of C Language
57Programming Language ? CTutorial Introduction
- Variables and Arithmetic Expressions
58ExampleFahrenheit-Celsius table
59ExampleFahrenheit-Celsius Table
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() int fahr, celsius int lower, upper,
step lower 0 / lower limit of temperature
scale / upper 300 / upper limit / step
20 / step size / fahr lower while (fahr
lt upper) celsius 5 (fahr-32) /
9 printf("d\td\n", fahr, celsius) fahr fahr
step
60ExampleFahrenheit-Celsius Table
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() int fahr, celsius int lower, upper,
step lower 0 / lower limit of temperature
scale / upper 300 / upper limit / step
20 / step size / fahr lower while (fahr
lt upper) celsius 5 (fahr-32) /
9 printf("d\td\n", fahr, celsius) fahr fahr
step
61Statements
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() int fahr, celsius int lower, upper,
step lower 0 / lower limit of temperature
scale / upper 300 / upper limit / step
20 / step size / fahr lower while (fahr
lt upper) celsius 5 (fahr-32) /
9 printf("d\td\n", fahr, celsius) fahr fahr
step
Individual statements are terminated by
semicolons.
62Variable Declarations
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() int fahr, celsius int lower, upper,
step lower 0 / lower limit of temperature
scale / upper 300 / upper limit / step
20 / step size / fahr lower while (fahr
lt upper) celsius 5 (fahr-32) /
9 printf("d\td\n", fahr, celsius) fahr fahr
step
- In C, all variables must be declared before they
are used. - Usually declared at the beginning of the function
before any executable statements. - A declaration announces the properties of
variables it consists of a name and a list of
variables.
63Variable Declarations
- The integer data type.
- What is its range?
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() int fahr, celsius int lower, upper,
step lower 0 / lower limit of temperature
scale / upper 300 / upper limit / step
20 / step size / fahr lower while (fahr
lt upper) celsius 5 (fahr-32) /
9 printf("d\td\n", fahr, celsius) fahr fahr
step
64Integer
int fahr, celsius int lower, upper, step
- The range of integer depends on machine
- 16-bit machine
- ?32768 ? 32767
- 32-bit machine
- ?2147483646 ? 2147483647
65Integer
int fahr, celsius int lower, upper, step
- The range of integer depends on machine
INT_MIN ? INT_MAX
66Integer
int fahr, celsius int lower, upper, step
- The range of integer depends on machine
INT_MIN ? INT_MAX
67Float
float fahr, celsius float lower, upper, step
- A float number is typically a 32-bit quantity,
with at least six significant digits and
magnitude generally between about 10-38 and 1038
FLT_MIN ? FLT_MAX
68Fundamental Types
Type Size
char 1 byte
short 2 bytes
int 4 bytes
long 4 bytes
float 4 bytes
double 8 bytes
Machine Dependent
69Fundamental Types in Microsoft C
Type Size
char 1 byte
short 2 bytes
int 4 bytes
long 4 bytes
float 4 bytes
double 8 bytes
Type Size
char 1 byte
short 2 bytes
int 4 bytes
long 4 bytes
float 4 bytes
double 8 bytes
70Assignment Statements
Assign a value computed from an expression to a
variable.
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() int fahr, celsius int lower, upper,
step lower 0 / lower limit of temperature
scale / upper 300 / upper limit / step
20 / step size / fahr lower while (fahr
lt upper) celsius 5 (fahr-32) /
9 printf("d\td\n", fahr, celsius) fahr fahr
step
71While Loop
while( cond_expression )statement
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() int fahr, celsius int lower, upper,
step lower 0 / lower limit of temperature
scale / upper 300 / upper limit / step
20 / step size / fahr lower while (fahr
lt upper) celsius 5 (fahr-32) /
9 printf("d\td\n", fahr, celsius) fahr fahr
step
The body of a while loop is repeatedly executed
as long as the condition expression is tested
true.
72Arithmetical Operations
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() int fahr, celsius int lower, upper,
step lower 0 / lower limit of temperature
scale / upper 300 / upper limit / step
20 / step size / fahr lower while (fahr
lt upper) celsius 5 (fahr-32) /
9 printf("d\td\n", fahr, celsius) fahr fahr
step
Can it be celsius 5/9(fahr-32)? Why?
73printf ? General-Purpose Output Formatting
Function
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() int fahr, celsius int lower, upper,
step lower 0 / lower limit of temperature
scale / upper 300 / upper limit / step
20 / step size / fahr lower while (fahr
lt upper) celsius 5 (fahr-32) /
9 printf("d\td\n", fahr, celsius) fahr fahr
step
74printf ? General-Purpose Output Formatting
Function
format string
printf("d\td\n", fahr, celsius)
75printf ? General-Purpose Output Formatting
Function
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() int fahr, celsius int lower, upper,
step lower 0 / lower limit of temperature
scale / upper 300 / upper limit / step
20 / step size / fahr lower while (fahr
lt upper) celsius 5 (fahr-32) /
9 printf("d\td\n", fahr, celsius) fahr fahr
step
Left aligned
76printf ? General-Purpose Output Formatting
Function
Synopsis include ltstdio.hgtint printf(const
char format, ...) Description printf writes
to the standard output stream using putchar,
under control of the string pointed to by format
that specifies how subsequent arguments are
converted for output. If there are insufficient
arguments for the format, the behavior is
undefined. If the format is exhausted while
arguments remain, the excess arguments are
evaluated but are otherwise ignored. printf
returns number of characters transmitted, or a
negative value if an output or encoding error
occurred.
77printf ? General-Purpose Output Formatting
Function
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() int fahr, celsius int lower, upper,
step lower 0 / lower limit of temperature
scale / upper 300 / upper limit / step
20 / step size / fahr lower while (fahr
lt upper) celsius 5 (fahr-32) /
9 printf("3d 6d\n", fahr, celsius) fahr
fahr step
Right aligned
78ExampleFahrenheit-Celsius Table (Float)
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() float fahr, celsius float lower,
upper, step lower 0 / lower limit of
temperature scale / upper 300 / upper limit
/ step 20 / step size / fahr
lower while (fahr lt upper) celsius
(5.0/9.0) (fahr-32.0) printf("3.0f 6.1f\n",
fahr, celsius) fahr fahr step
79ExampleFahrenheit-Celsius Table (Float)
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() float fahr, celsius float lower,
upper, step lower 0 / lower limit of
temperature scale / upper 300 / upper limit
/ step 20 / step size / fahr
lower while (fahr lt upper) celsius
(5.0/9.0) (fahr-32.0) printf("3.0f 6.1f\n",
fahr, celsius) fahr fahr step
80More on printf
81Statements
- Simple Statements
- lower 0
- upper 300
- step 20
- fahr lower
- Null Statement
- // a null statement
- Compound Statements
-
- celsius (5.0/9.0) (fahr-32.0)
- printf("3.0f 6.1f\n", fahr, celsius)
- fahr fahr step
-
4 simple statements
1 compound statement
82While Statements
while( cond_expression ) statement
a simple statement a null statement a compound
statements
The statement can be
83Program Indentation
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() float fahr, celsius float lower,
upper, step lower 0 / lower limit of
temperature scale / upper 300 / upper limit
/ step 20 / step size / fahr
lower while (fahr lt upper) celsius
(5.0/9.0) (fahr-32.0) printf("3.0f 6.1f\n",
fahr, celsius) fahr fahr step
84Program Indentation
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() float fahr, celsius float lower,
upper, step lower 0 / lower limit of
temperature scale / upper 300 / upper limit
/ step 20 / step size / fahr
lower while (fahr lt upper) celsius
(5.0/9.0) (fahr-32.0) printf("3.0f 6.1f\n",
fahr, celsius) fahr fahr step
Statement
Statement
Statement
Statement
Statement
Statement
Statement
85Program Indentation
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() float fahr, celsius float lower,
upper, step lower 0 / lower limit of
temperature scale / upper 300 / upper limit
/ step 20 / step size / fahr
lower while (fahr lt upper) celsius
(5.0/9.0) (fahr-32.0) printf("3.0f 6.1f\n",
fahr, celsius) fahr fahr step
Statement
Statement
Statement
Statement
Statement
Statement
Statement
Statement
Statement
86Program Indentation
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() float fahr, celsius float lower,
upper, step lower 0 / lower limit of
temperature scale / upper 300 / upper limit
/ step 20 / step size / fahr
lower while (fahr lt upper) celsius
(5.0/9.0) (fahr-32.0) printf("3.0f 6.1f\n",
fahr, celsius) fahr fahr step
Statement
Statement
Statement
Statement
Statement
Statement
87Programming Language ? CTutorial Introduction
88ExampleFahrenheit-Celsius Table
include ltstdio.hgt / print Fahrenheit-Celsius
table for fahr 0, 20, ..., 300
/ main() float fahr, celsius float lower,
upper, step lower 0 / lower limit of
temperature scale / upper 300 / upper limit
/ step 20 / step size / fahr
lower while (fahr lt upper) celsius
(5.0/9.0) (fahr-32.0) printf("3.0f 6.1f\n",
fahr, celsius) fahr fahr step
There are many ways to do the same task.
89ExampleFahrenheit-Celsius Table
include ltstdio.hgt / print Fahrenheit-Celsius
table / main() int fahr for (fahr 0 fahr
lt 300 fahr fahr 20) printf("3d 6.1f\n",
fahr, (5.0/9.0)(fahr-32))
90The for Statement
include ltstdio.hgt / print Fahrenheit-Celsius
table / main() int fahr for (fahr 0 fahr
lt 300 fahr fahr 20) printf("3d 6.1f\n",
fahr, (5.0/9.0)(fahr-32))
for ( init-exp cond-exp loop-exp ) statement
91for vs. while
- The choice between while and for is arbitrary,
based on which seems clearer. -
- The for is usually appropriate for loops in which
the initialization and increment are single
statements and logically related.
92Exercises
- Modify the temperature conversion program to
print the table in reverse order, that is, from
300 degrees to 0.
93Exercises
- Using only the techniques you learned, write a
program that calculates the squares and cubes of
the number from 0 to 10 and used tabs to print
the following table of values.
94Programming Language ? CTutorial Introduction
95define ? Text Substitution
define name replacement list
Example
define PI 3.1415926
96ExampleFahrenheit-Celsius Table
include ltstdio.hgt define LOWER 0 / lower
limit of table / define UPPER 300 / upper
limit / define STEP 20 / step size / /
print Fahrenheit-Celsius table / main() int
fahr for (fahr LOWER fahr lt UPPER fahr
fahr STEP) printf("3d 6.1f\n", fahr,
(5.0/9.0)(fahr-32))
97Programming Language ? CTutorial Introduction
- Character
- Input and Output
98getchar() putchar()
define EOF (-1)
Synopsis include ltstdio.hgt int
getchar(void) Description getchar reads a
single character from the standard input
stream. If the stream is at end-of-file or a read
error occurs, getchar returns EOF.
Synopsis include ltstdio.hgt int putchar(int
c) Description putchar writes the character c
to the standard output stream. putchar returns
the character written. If a write error occurs,
putchar returns EOF.
99Example Typewriter (Version 1)
! not equal
include ltstdio.hgt / copy input to output 1st
version / main() int c c
getchar() while (c ! EOF)
putchar(c) c getchar()
100Example Typewriter (Version 2)
The return value of an assignment statement is
value of the variable after the assignment.
include ltstdio.hgt / copy input to output 2st
version / main() int c c
getchar() while ((c getchar()) !
EOF) putchar(c)
simple statement
101Example Typewriter (Version 2)
The return value of an assignment statement is
value of the variable after the assignment.
include ltstdio.hgt / copy input to output 2st
version / main() int c c
getchar() while ((c getchar()) ! EOF)
putchar(c)
102Example Character Counting (Version 1)
increment
include ltstdio.hgt / count characters in input
1st version / main() long nc nc
0 while (getchar() ! EOF) nc printf("ld\
n", nc)
simple statement
103Example Character Counting (Version 1)
increment
include ltstdio.hgt / count characters in input
1st version / main() long nc nc
0 while (getchar() ! EOF) nc printf("ld\n"
, nc)
104Example Character Counting (Version 2)
include ltstdio.hgt / count characters in input
2nd version / main() double nc for
(nc 0 getchar() ! EOF nc)
printf(".0f\n", nc)
null statement
105Example Character Counting (Version 2)
include ltstdio.hgt / count characters in input
2nd version / main() double nc for
(nc 0 getchar() ! EOF nc)
printf(".0f\n", nc)
106Example Line Counting
equals to
include ltstdio.hgt / count lines in input
/ main() int c, nl nl 0 while
((c getchar()) ! EOF) if (c '\n')
nl printf("d\n", nl)
if ( expression ) statement if ( expression )
statement else statement
simple statement
107Example Line Counting
include ltstdio.hgt / count lines in input
/ main() int c, nl nl 0 while
((c getchar()) ! EOF) if (c '\n')
nl printf("d\n", nl)
simple statement
108Example Line Counting
include ltstdio.hgt / count lines in input
/ main() int c, nl nl 0 while
((c getchar()) ! EOF) if (c '\n')
nl printf("d\n", nl)
Can we move to here?
simple statement
109Example Word Counting
OIIIIOIIOIOOOIIIIO OIIIII
110Example Word Counting
include ltstdio.hgt define IN 1 / inside a
word / define OUT 0 / outside a word / /
count lines, words, and characters in input
/ main() int c, nl, nw, nc, state
state OUT nl nw nc 0 while ((c
getchar()) ! EOF) nc if (c '\n')
nl if (c ' ' c '\n' c '\t')
state OUT else if (state OUT) state
IN nw printf("d d
d\n", nl, nw, nc)
111Exercises
- Write a program to print a histogram of the
frequencies of different characters in its input.
112Programming Language ? CTutorial Introduction
113Arrays
score
Array is a data structure that stores contiguous
data elements of the same type.
address
distance
Examples
int score50 char address50 double
distance50
114Arrays
Examples
int score50 char address50 double
distance50
score
address
distance
115Arrays
Examples
int score50 char address50 double
distance50
score
address
distance
116Arrays
Examples
int score50 char address50 double
distance50
score
address
distance
117Example Digit Counting
logical and operator
logical or operator
include ltstdio.hgt / count digits, white space,
others / main() int c, i, nwhite,
nother int ndigit10 nwhite nother
0 for (i 0 i lt 10 i) ndigiti
0 while ((c getchar()) ! EOF) if (c gt
'0' c lt '9') ndigitc-'0' else if (c
' ' c '\n' c '\t') nwhite else
nother printf("digits ") for (i 0 i lt
10 i) printf(" d", ndigiti) printf(",
white space d, other d\n", nwhite,
nother)
118Example Digit Counting
include ltstdio.hgt / count digits, white space,
others / main() int c, i, nwhite,
nother int ndigit10 nwhite nother
0 for (i 0 i lt 10 i) ndigiti
0 while ((c getchar()) ! EOF) if (c gt
'0' c lt '9') ndigitc-'0' else if (c
' ' c '\n' c '\t') nwhite else
nother printf("digits ") for (i 0 i lt
10 i) printf(" d", ndigiti) printf(",
white space d, other d\n", nwhite,
nother)
119Example Digit Counting
include ltstdio.hgt / count digits, white space,
others / main() int c, i, nwhite,
nother int ndigit10 nwhite nother
0 for (i 0 i lt 10 i) ndigiti
0 while ((c getchar()) ! EOF) if (c gt
'0' c lt '9') ndigitc-'0' else if (c
' ' c '\n' c '\t') nwhite else
nother printf("digits ") for (i 0 i lt
10 i) printf(" d", ndigiti) printf(",
white space d, other d\n", nwhite,
nother)
120Example Digit Counting
include ltstdio.hgt / count digits, white space,
others / main() int c, i, nwhite,
nother int ndigit10 nwhite nother
0 for (i 0 i lt 10 i) ndigiti
0 while ((c getchar()) ! EOF) if (c gt
'0' c lt '9') ndigitc-'0' else if (c
' ' c '\n' c '\t') nwhite else
nother printf("digits ") for (i 0 i lt
10 i) printf(" d", ndigiti) printf(",
white space d, other d\n", nwhite,
nother)
121if Statement
The dangling else problem
if (condition1) statement1 else if (condition2)
statement2 else if (condition3)
statement3 ... else statementn
122Exercises
- Write a program that prints its input one word
per line. - Write a program that prints only a single white
character between words.
123Exercises
- Write a program to print a histogram of the
frequencies of different characters in its input.
124Programming Language ? CTutorial Introduction
125Functions we have used
126Functions
- A way to encapsulate some computation.
- Similar to a subroutine or function in Fortran,
or a procedure or function in Pascal. - A properly designed function allow us to
- know what is done, but
- ignore how a job is done
127Function Definition
return-type function-name(parameter declarations,
if any) declarations statements
128Function Definition
If absent, return type is considered int.
return-type function-name(parameter declarations,
if any) declarations statements
Called formal parameters
129Example Power
include ltstdio.hgt int power(int m, int n) /
test power function / main() int i
for (i 0 i lt 10 i) printf("d d
d\n", i, power(2,i), power(-3,i)) return
0 / power raise base to n-th power n gt 0
/ int power(int base, int n) int i, p
p 1 for (i 1 i lt n i) p p
base return p
? forward reference
130Example Power
include ltstdio.hgt int power(int m, int n) /
test power function / main() int i
for (i 0 i lt 10 i) printf("d d
d\n", i, power(2,i), power(-3,i)) return
0 / power raise base to n-th power n gt 0
/ int power(int base, int n) int i, p
p 1 for (i 1 i lt n i) p p
base return p
131Example Power (Version 2)
/ power raise base to n-th power n gt 0
version 2 / int power(int base, int n)
int i, p for (p 1 n lt 0 --n) p p
base return p
/ power raise base to n-th power n gt 0
/ int power(int base, int n) int i, p
p 1 for (i 1 i lt n i) p p
base return p
132Example Power (Version 2)
/ power raise base to n-th power n gt 0
version 2 / int power(int base, int n)
int i, p for (p 1 n lt 0 --n) p p
base return p
. . . . . . . . . . main() int n, val
n 5 val power(2, n)
printf("vald nd d\n", val, n) . . . . . .
. . . .
n?
133Call by Value
- In C, all function arguments are passed by
value. - the called function is given the values of its
arguments in temporary variables rather than the
originals. - Modifying the values of formal parameters doesnt
effect the value of originals - How about if a function has arrays as its
parameters?
134Programming Language ? CTutorial Introduction
135Strings in C
- Null-terminated string ? In C, a string is stored
as an array of characters containing the
characters in the string and terminated with a
'\0' to mark the end.
str
Example
char str"hello\n"
136Strings in C
137Example Print the Longest Line
while (there's another line) if (it's longer
than the previous longest) (save it)
(save its length) print longest line
138Example Print the Longest Line
Implement a getline() function which returns the
length of the line read.
while (there's another line) if (it's longer
than the previous longest) (save it)
(save its length) print longest line
Implement a copy() function to save string.
139Example Print the Longest Line
logical and operator
/ getline read a line into s, return length
/ int getline(char s,int lim) int c,
i for (i0 i lt lim-1 (cgetchar())!EOF
c!'\n' i) si c if (c '\n')
si c i si
'\0' return i
140Example Print the Longest Line
logical and operator
Simplification
/ getline read a line into s, return length
/ int getline(char s,int lim) int c,
i for (i0 i lt lim-1 (cgetchar())!EOF
c!'\n' i) si c if (c '\n')
si c i si
'\0' return i
si c
si c
141Example Print the Longest Line
logical and operator
/ getline read a line into s, return length
/ / simplified version / int getline(char
s,int lim) int c, i for (i0 i lt
lim-1 (cgetchar())!EOF c!'\n')
si c if (c '\n') si c
si '\0' return i
142Example Print the Longest Line
/ copy copy 'from' into 'to' assume to is big
enough / void copy(char to, char from)
int i i 0 while ((toi fromi)
! '\0') i
143Example Print the Longest Line
include ltstdio.hgt define MAXLINE 1000 /
maximum input line length / int getline(char
line, int maxline) void copy(char to, char
from) / print the longest input line
/ main() int len / current line
length / int max / maximum length
seen so far / char lineMAXLINE / current
input line / char longestMAXLINE / longest
line saved here / max 0 while ((len
getline(line, MAXLINE)) gt 0) if (len gt max)
max len copy(longest, line) if
(max gt 0) / there was a line / printf("s",
longest) return 0 . . . . . . . . . . . .
144Example Print the Longest Line
145Example Print the Longest Line
146Example Print the Longest Line
147Exercises
- Write a function getlinestr() by modifying
getline() described above so that the string read
doesnt include '\n'. - Write a function reverse(char s) that reverses
the character string s. - Write a function int strlen(char s) that
returns the number of characters in string s. - Write a program to demonstrate that the about
three functions you wrote are correct.
148Exercises
- Write a function int atoi(char s) that converts
a signed decimal numeric string, assumed the
syntax is correct, to an integer, and write a
program to verify the correctness of your
program. - Write a program that reads two integers from the
standard input stream and output their minimum
and maximum to the standard output stream. You
cant use library function scanf() to read data. - (Take home) Write a function void itoa(int n,
char s) that converts a signed integer to a
signed decimal numeric string, and write a
program to verify the correctness of your program.
149Programming Language ? CTutorial Introduction
- External Variables and Scope
150Local (Private) Variables
include ltstdio.hgt define MAXLINE 1000 /
maximum input line length / int getline(char
line, int maxline) void copy(char to, char
from) / print the longest input line
/ main() int len / current
line length / int max / maximum
length seen so far / char lineMAXLINE
/ current input line / char
longestMAXLINE / longest line saved here /
max 0 while ((len getline(line,
MAXLINE)) gt 0) if (len gt max)
max len copy(longest, line)
if (max gt 0) / there was a line
/ printf("s", longest) return 0
/ getline read a line into s, return length
/ / simplified version / int getline(char
s,int lim) int c, i for (i0 i lt
lim-1 (cgetchar())!EOF c!'\n')
si c if (c '\n') si c
si '\0' return i / copy copy
'from' into 'to' assume to is big enough / void
copy(char to, char from) int i i
0 while ((toi fromi) ! '\0') i
151Local (Private) Variables
include ltstdio.hgt define MAXLINE 1000 /
maximum input line length / int getline(char
line, int maxline) void copy(char to, char
from) / print the longest input line
/ main() int len / current
line length / int max / maximum
length seen so far / char lineMAXLINE
/ current input line / char
longestMAXLINE / longest line saved here /
max 0 while ((len getline(line,
MAXLINE)) gt 0) if (len gt max)
max len copy(longest, line)
if (max gt 0) / there was a line
/ printf("s", longest) return 0
/ getline read a line into s, return length
/ / simplified version / int getline(char
s,int lim) int c, i for (i0 i lt
lim-1 (cgetchar())!EOF c!'\n')
si c if (c '\n') si c
si '\0' return i / copy copy
'from' into 'to' assume to is big enough / void
copy(char to, char from) int i i
0 while ((toi fromi) ! '\0') i
- Each local variable comes into existence only
when the function is called, and disappears when
the function is exited. - Hence, also known as automatic variables.
152Global (Extern) Variables
- Global variables that are defined external to all
functions can be accessed by name by any
function. - Furthermore, they remain in existence permanently.
153Example Print the Longest Line
include ltstdio.hgt define MAXLINE 1000 /
maximum input line size / int max
/ maximum length seen so far / char
lineMAXLINE / current input line / char
longestMAXLINE / longest line saved here
/ int getline(void) void copy(void) / print
longest input line specialized version
/ main() . . . . . . . . . . . . /
getline specialized version / int
getline(void) . . . . . . . . . . . .
/ copy specialized version / void
copy(void) . . . . . . . . . . . .
defined external to all functions
154Example Print the Longest Line
include ltstdio.hgt define MAXLINE 1000 /
maximum input line size / int max
/ maximum length seen so far / char
lineMAXLINE / current input line / char
longestMAXLINE / longest line saved here
/ int getline(void) void copy(void) / print
longest input line specialized version
/ main() . . . . . . . . . . . . /
getline specialized version / int
getline(void) . . . . . . . . . . . .
/ copy specialized version / void
copy(void) . . . . . . . . . . . .
defined external to all functions
/ print longest input line specialized version
/ main() int len extern int max
extern char longest max 0 while
((len getline()) gt 0) if (len gt max)
max len copy()
if (max gt 0) / there was a line /
printf("s", longest) return 0
155Example Print the Longest Line
include ltstdio.hgt define MAXLINE 1000 /
maximum input line size / int max
/ maximum length seen so far / char
lineMAXLINE / current input line / char
longestMAXLINE / longest line saved here
/ int getline(void) void copy(void) / print
longest input line specialized version
/ main() . . . . . . . . . . . . /
getline specialized version / int
getline(void) . . . . . . . . . . . .
/ copy specialized version / void
copy(void) . . . . . . . . . . . .
defined external to all functions
/ getline specialized version / int
getline(void) int c, i extern char
line for (i 0 i lt MAXLINE - 1
(cgetchar()) ! EOF c ! '\n' i)
linei c if (c '\n')
linei c i linei
'\0' return i
156Example Print the Longest Line
include ltstdio.hgt define MAXLINE 1000 /
maximum input line size / int max
/ maximum length seen so far / char
lineMAXLINE / current input line / char
longestMAXLINE / longest line saved here
/ int getline(void) void copy(void) / print
longest input line specialized version
/ main() . . . . . . . . . . . . /
getline specialized version / int
getline(void) . . . . . . . . . . . .
/ copy specialized version / void
copy(void) . . . . . . . . . . . .
defined external to all functions
/ copy specialized version / void
copy(void) int i extern char line,
longest i 0 while ((longesti
linei) ! '\0') i
157Example Print the Longest Line
The declarations for referencing external
variables are redundant if they are defined in
the header part of the same file .
/ print longest input line specialized version
/ main() int len extern int max
extern char longest max 0 while
((len getline()) gt 0) if (len gt max)
max len copy()
if (max gt 0) / there was a line /
printf("s", longest) return 0
include ltstdio.hgt define MAXLINE 1000 /
maximum input line size / int max
/ maximum length seen so far / char
lineMAXLINE / current input line / char
longestMAXLINE / longest line saved here
/ int getline(void) void copy(void) / print
longest input line specialized version
/ main() . . . . . . . . . . . . /
getline specialized version / int
getline(void) . . . . . . . . . . . .
/ copy specialized version / void
copy(void) . . . . . . . . . . . .
defined external to all functions
/ getline specialized version / int
getline(void) int c, i extern char
line for (i 0 i lt MAXLINE - 1
(cgetchar()) ! EOF c ! '\n' i)
linei c if (c '\n')
linei c i linei
'\0' return i
/ copy specialized version / void
copy(void) int i extern char line,
longest i 0 while ((longesti
linei) ! '\0') i
158Example Print the Longest Line (Multiple Source
Files) ? I
/ main.c / include ltstdio.hgt define MAXLINE
1000 / maximum input line size / int max
/ maximum length seen so far
/ char lineMAXLINE / current input line
/ char longestMAXLINE / longest line saved
here / int getline(void) void copy(void) /
print longest input line specialized version
/ main() . . . . . . . . . . . .
/ functions.c / / getline specialized
version / int getline(void) . . . . . . .
. . . . . / copy specialized version
/ void copy(void) . . . . . . . . . . . .
159Example Print the Longest Line (Multiple Source
Files) ? I
/ print longest input line specialized version
/ main() int len extern int max
extern char longest max 0 while
((len getline()) gt 0) if (len gt max)
max len copy()
if (max gt 0) / there was a line /
printf("s", longest) return 0
/ main.c / include ltstdio.hgt define MAXLINE
1000 / maximum input line size / int max
/ maximum length seen so far
/ char lineMAXLINE / current input line
/ char longestMAXLINE / longest line saved
here / int getline(void) void copy(void) /
print longest input line specialized version
/ main() . . . . . . . . . . . .
/ getline specialized version / int
getline(void) int c, i extern char
line for (i 0 i lt MAXLINE - 1
(cgetchar()) ! EOF c ! '\n' i)
linei c if (c '\n')
linei c i linei
'\0' return i
Must be kept.
/ functions.c / / getline specialized
version / int getline(void) . . . . . . .
. . . . . / copy specialized version
/ void copy(void) . . . . . . . . . . . .
/ copy specialized version / void
copy(void) int i extern char line,
longest i 0 while ((longesti
linei) ! '\0') i
Must be kept.
160Example Print the Longest Line (Multiple Source
Files) ? II
/ main.c / include ltstdio.hgt define MAXLINE
1000 / maximum input line size / int max
/ maximum length seen so far
/ char lineMAXLINE / current input line
/ char longestMAXLINE / longest line saved
here / int getline(void) void copy(void) /
print longest input line specialized version
/ main() . . . . . . . . . . . .
/ externdef.h / define MAXLINE 1000 /
maximum input line size / extern int max
/ maximum length seen so far / extern char
line / current input line / extern char
longest / longest line saved here /
/ functions.c / include ltstdio.hgt include
"externdef.h" / getline specialized version
/ int getline(void) . . . . . . . . . . .
. / copy specialized version / void
copy(void) . . . . . . . . . . . .
161Example Print the Longest Line (Multiple Source
Files) ? II
/ main.c / include ltstdio.hgt define MAXLINE
1000 / maximum input line size / int max
/ maximum length seen so far
/ char lineMAXLINE / current input line
/ char longestMAXLINE / longest line saved
here / int getline(void) void copy(void) /
print longest input line specialized version
/ main() . . . . . . . . . . . .
/ externdef.h / define MAXLINE 1000 /
maximum input line size / extern int max
/ maximum length seen so far / extern char
line / current input line / extern char
longest / longest line saved here /
/ getline specialized version / int
getline(void) int c, i extern char
line for (i 0 i lt MAXLINE - 1
(cgetchar()) ! EOF c ! '\n' i)
linei c if (c '\n')
linei c i linei
'\0' return i
/ functions.c / include ltstdio.hgt include
"externdef.h" / getline specialized version
/ int getline(void) . . . . . . . . . . .
. / copy specialized version / void
copy(void) . . . . . . . . . . . .
/ copy specialized version / void
copy(void) int i extern char line,
longest i 0 while ((longesti
linei) ! '\0') i
162Exercise
- (Take home) Write a program to remove all
comments from a C program. Don't forget to handle
quoted strings and character constants properly.
C comments don't nest.