the c programming language: introduction fall 2003, jen-chang liu
Post on 19-Dec-2015
229 views
TRANSCRIPT
![Page 1: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/1.jpg)
The C programming language:Introduction
Fall 2003, Jen-Chang Liu
![Page 2: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/2.jpg)
Position of C
Hardwaremachines
MS WindowsUnixLinux
shelldesktop dos
applications
machinelanguage
High-levellanguage
compiler
C, C++Pascal,..
HumanNatural language
![Page 3: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/3.jpg)
Introduction
B -> C C is a general-purpose
programming language Developed on UNIX system by D.
Ritchie Portable, independent of any machine
architecture OS, C compiler and all UNIX application
were written in C
![Page 4: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/4.jpg)
Introduction (cont.)
A relatively small language ANSI C standard in 1988
The first edition of The C Programming Language was usually used as a reference manual of C
American National Standards Institute Develop an unambiguous and machine-
independent definition of C
![Page 5: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/5.jpg)
Tutorial Started by learning by examples… Please try the examples on your own
![Page 6: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/6.jpg)
如何產生可執行程式?
C sourceCodeC 原始程式
Compiler編譯器
ExecutableCode可執行碼
Turbo C 2.01整合式編譯環境
文字檔形式*.c
執行檔形式*.exe
Visual C++
![Page 7: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/7.jpg)
Compiler Unix: cc, gcc(GNU C compiler)
gcc has PC version, you may try to install it Windows: Visual C++, Borland C++
編譯器
aaa.ccompiler aaa.obj
bbb.ccompiler bbb.obj
abc.exe
…
link
![Page 8: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/8.jpg)
Example 1
#include <stdio.h>
main( ) { printf("Hello, world\n"); }
functions
Standard input/output library
printfscanf…
argumentsBody ofmain func
Terminatorof
statement
函式庫
![Page 9: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/9.jpg)
C programming concept
Controlunit
tape
read/write head
…
symbols
state of the machines
Turing machine
printf(“Hello world!”);
![Page 10: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/10.jpg)
Notes Blank words( 空白,換行 ) are irrelevant in C C is well-defined language
with a set of keywords with a set of functions
Case sensitive: error typing is not allowed
inputfunction
output
![Page 11: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/11.jpg)
Example 1 (cont.) “Hello, world\n”
Example 2
Character strings(string constant)
Newline character\t: tab, \b: backspace, \\: \
![Page 12: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/12.jpg)
Program components Function
Contains statements that specify the computing operations to be done
Variable Store values for computing
![Page 13: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/13.jpg)
Outline
Variable and Arithmetic Expression The For Statement Symbolic Constants Character Input and Output Arrays Functions Arguments – Call by Value Character Arrays External Variables and Scope
![Page 14: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/14.jpg)
上課程式實做繳交規定program submission in class
每次上課後將當天實做之程式繳交 方式 : email
Subject: work 日期 學號 例: work1027 93321001 只交上 .c 檔便可
繳交程式必須加上註解 (comments) ,包括 姓名,學號 程式目的 每行的作用
![Page 15: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/15.jpg)
Prog2-1// 劉震昌 , 93xx0xx// 程式目的:測試整數變數並印出#include <stdio.h>void main(void){ int i; /* 變數宣告 declaration */ i=2; /* 設定變數 */ printf(“This is my %dnd C program”, i); // 印出
訊息}
![Page 16: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/16.jpg)
變數宣告與設定 宣告:告知編譯器 data type 與 變數名稱
Ex. int i;
變數設定
變數名稱可以任取,最好取有意義的文字( 第一個字不可為數字 )
變數型態
i = 2; // 將 i 設成 2
![Page 17: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/17.jpg)
Data types of Variables
int integershort short integerlong long integerchar characterfloat floating pointdouble double-precision
case-sensitivekeywords
Integer withmachine-dependentsizes
![Page 18: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/18.jpg)
Prog4-0 (p. 4-10)
#include <stdio.h>void main(void){ printf(“Size of char : %d\n”, sizeof(char)); printf(“Size of int : %d\n”, sizeof(int)); printf(“Size of short : %d\n”, sizeof(short)); printf(“Size of float : %d\n”, sizeof(float)); printf(“Size of double: %d\n”, sizeof(doubl
e));}
![Page 19: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/19.jpg)
Test the range of (short)int#include <stdio.h>void main(void){
short i;i=32767;printf("i=%d\n", i);i=32768;printf("i=%d\n", i);
}
![Page 20: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/20.jpg)
Variables and arithmetic expression (1.2)
C = (5/9)(F-32) 攝氏 -華氏轉換 Try to output the right table using printf
0 -1720 -640 460 1580 26100 37120 48140 60160 71180 82200 93220 104240 115260 126280 137300 148
F C
![Page 21: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/21.jpg)
0 -1720 -640 460 1580 26100 37120 48140 60160 71180 82200 93220 104240 115260 126280 137300 148
/* temperature version 0 */#include <stdio.h>
main(){ printf(“0 -17\n"); printf(“20 -6\n"); printf(“40 4\n”); /* … */}
![Page 22: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/22.jpg)
0 -1720 -640 460 1580 26100 37120 48140 60160 71180 82200 93220 104240 115260 126280 137300 148
/* temperature version 1 */#include <stdio.h>
main(){ int F, C;
F = 0; C = 5 * (F-32) / 9; printf("%d %d\n", F, C);
F = F+20; C = 5 * (F-32) / 9; printf("%d %d\n", F, C);
/* … */}
/*algorithm */F=0 -> CF+20 -> CF+20 -> C…
![Page 23: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/23.jpg)
Arithmetic expression C = 5*(F-32)/9;
C = 5/9*(F-32);
Expressions are evaluated in this direction one-by-one
=0Because their type is integer,division will truncate the remainder
![Page 24: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/24.jpg)
Equation in C F = F + 20;
F 無解?
F + 20 Temp
F
![Page 25: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/25.jpg)
More about printf printf("%d\t%d\n", F, C);
Variables should be properly initialized C does not have build-in input or output
Call the function printf and scanf
The current value of the variable will be printed on display
![Page 26: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/26.jpg)
More about printf Format numeric output
%d
tight output
0
20
40
60
120
%3d
0
20
40
60
120
Right justified
![Page 27: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/27.jpg)
Course break
http://alg.csie.ncnu.edu.tw/course/cprogram/introduction.htm
![Page 28: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/28.jpg)
0 -1720 -640 460 1580 26100 37120 48140 60160 71180 82200 93220 104240 115260 126280 137300 148
/* temperature version 2 */#include <stdio.h>
main(){ float F, C;
F = 0.0; C = 5.0 * (F-32.0) / 9.0; printf(“%f %f\n", F, C);
F = F+20.0; C = 5.0 * (F-32.0) / 9.0; printf("%f %f\n", F, C);
/* … */}
/*algorithm */F=0 -> CF+20 -> CF+20 -> C…
![Page 29: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/29.jpg)
Floating point C = 5.0/9.0 *(F-32.0);
(F-32) : integer will convert to float for computation
C = 5/9 *(F-32.0); What will happen?
printf("%3.0f\t%6.1f\n", F, C);
Evaluateorder
整數位數 小數位數
![Page 30: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/30.jpg)
Floating point (cont.) Many print-out form
others
%f%6f%.2f%6.2f
%o otcal%x hexadecimal%c character%s character string%% %
![Page 31: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/31.jpg)
流程控制: while loop 迴圈
Testcondition
Enter loop
Yes (non-0)
ExecuteLoop body
noexit
F=0F=F+20F=F+20…
F=F+20
0 -1720 -640 460 1580 26100 37120 48140 60160 71180 82200 93220 104240 115260 126280 137300 148
F=0
F=F+20
F<=300
![Page 32: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/32.jpg)
while loop 迴圈
F = 0;while (F <= 300){ F = F+20;}
Testcondition
Enter loop
Yes(non-0)
ExecuteLoop body
noexit
/* C Language */
F=0
F<=300
F=F+20
![Page 33: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/33.jpg)
#include <stdio.h>
/* temperature version 3.0 */
main(){ int fahr, celsius; int lower, upper, step;
lower = 0; /* lower limit of temperature table */ upper = 300; /* upper limit */ step = 20; /* step size */
fahr = lower; while (fahr <= upper){ celsius = 5*(fahr-32)/9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; }}
Variable Declarationint = integer
Variableassignment
whileloop
![Page 34: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/34.jpg)
#include <stdio.h>
/* print F-C table */
main(){ float fahr, celsius; int lower, upper, step;
lower = 0; /* lower limit of temperature table */ upper = 300; /* upper limit */ step = 20; /* step size */
fahr = lower; while (fahr <= upper){ celsius = 5.0/9.0 *(fahr-32.0); printf("%3.0f\t%6.1f\n", fahr, celsius); fahr = fahr + step; }}
floating pointversion of ex3.
Floating point costant
![Page 35: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/35.jpg)
Exercise 1 Print the following numbers using while
loops11235813213455
![Page 36: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/36.jpg)
Outline
Variable and Arithmetic Expression The For Statement Symbolic Constants Character Input and Output Arrays Functions Arguments – Call by Value Character Arrays External Variables and Scope
![Page 37: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/37.jpg)
for loop
for(initial ; loop test ; increment){ Loop body… … }
Testcondition
Enter loop
yes
ExecuteLoop body
noexit
initial value
ExecuteLoop increment
F=0
F=F+20;
F<=300
for(F=0; F<=300; F=F+20){ printf(“%d\n”, F);}
![Page 38: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/38.jpg)
for statement
#include <stdio.h>
/* print F-S table */main(){ int fahr;
for(fahr=0; fahr <= 300; fahr = fahr+20) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));}
initial increment
exit looptest
Type conversion
![Page 39: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/39.jpg)
Exercise 2 Print the following numbers using for
loops11235813213455
![Page 40: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/40.jpg)
Review of HW#2
*** *** **** ***** ****** ******* ******** ********* **********
#include<stdio.h>
main(){
int i,j;for(i=1;i<=10;i++){ /* 第一層迴圈
* 增加直行 */for(j=1;j<=i;j++){
printf("*"); /* 第二層迴圈 * 增加橫的 */
}printf("\n");
}}
![Page 41: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/41.jpg)
Review of HW#2
*** *** **** ***** ****** ******* ******** ********* **********
#include<stdio.h>main(){ int i,k; for(i=0;i<10;i++){ for(k=0;k<i;k++){ printf("*"); } printf("\n"); }}
![Page 42: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/42.jpg)
Review of HW#2
*** *** **** ***** ****** ******* ******** ********* **********
#include <stdio.h>
main (){ int A, B; for(A=1; A<=10; A= A+1){ B=0; printf("\n"); while(B<A){ printf("*"); B=B+1; } }}
![Page 43: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/43.jpg)
Review of HW#2
*** *** **** ***** ****** ******* ******** ********* **********
#include<stdio.h>
int main (){
int a,b;
for(a=0;a<10;a++){for(b=1;b<=a;b++)
printf("*", b);printf("*\n" ,a);
}return 0;
}
![Page 44: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/44.jpg)
Review of HW#2
*** *** **** ***** ****** ******* ******** ********* **********
#include <stdio.h>
int main(){
int a,b; a=10; b=11;
while(a>=0){ while(b<11){
printf("*");b=b+1;
} printf("\n"); b=a; a=a-1; } }
![Page 45: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/45.jpg)
Outline
Variable and Arithmetic Expression The For Statement Symbolic Constants Character Input and Output Arrays Functions Arguments – Call by Value Character Arrays External Variables and Scope
![Page 46: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/46.jpg)
Symbol constants
#include <stdio.h>
#define LOWER 0#define UPPER 300#define STEP 20
/* print F-S table */main(){ int fahr;
for(fahr=LOWER; fahr <= UPPER; fahr = fahr+STEP) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));}
Symbol name(Meaningful, easy to read)
Symbol value
Replace this symbol nameat compile time
![Page 47: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/47.jpg)
Outline Variable and Arithmetic Expression The For Statement Symbolic Constants Character Input and Output Arrays Functions Arguments – Call by Value Character Arrays External Variables and Scope
![Page 48: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/48.jpg)
Character I/O A text stream is a sequence of characters getchar() getch() getche() getc()
putchar(c) putch(c,stdout) putc(c,stdout)
I/O devices are also takenas files
輸入stdin
輸出stdout
![Page 49: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/49.jpg)
![Page 50: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/50.jpg)
Example: File copying
#include <stdio.h>
/* echo, version 1 */main(){ int c;
c=getchar(); while( c != EOF ){ putchar(c); c = getchar(); }}
not equal to
End Of File
A constant definedin stdio.hNOT the same asany char values
![Page 51: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/51.jpg)
Example: File copying EOF
Print the value of EOF
End of file
OS Keyboardterminationsignal(ctrl-Z)
#include <stdio.h>
main(){ printf("EOF = %d\n", EOF);}
![Page 52: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/52.jpg)
Exercise Modify your homework#2
1. Use symbol constant 2. Read keyboard input to decide the level of output
*** *** **** ***** ****** ******* ******** ********* **********
![Page 53: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/53.jpg)
Example: File copyingAssignment
c= getchar(); Assignment is an expression and has a valu
e, which is the value of the left hand side after assignment.
#include <stdio.h>main(){ int c;
while( (c=getchar()) != EOF ){ putchar(c); }}
Precedence of = and !=
![Page 54: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/54.jpg)
Example: Character counting
#include <stdio.h>
main(){ long nc; /* number of character */
nc = 0; while(getchar() != EOF) ++nc; printf("%ld\n",nc);}
Good namingConventionFor variables
nc = nc+1;
32-bitinteger
![Page 55: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/55.jpg)
Example : Character counting 2
#include <stdio.h>
main(){ double nc;
for(nc = 0; getchar()!= EOF; ++nc) ; printf("%.0f\n",nc);}
null statement
Increaserange
![Page 56: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/56.jpg)
Example: Line counting
#include <stdio.h>
/* count lines */main(){ int c, nl;
nl = 0; while( (c=getchar()) != EOF) if(c == '\n') ++nl; printf("%d\n", nl);}
Test condition
is equal to
條件測試characterconstant
ASCII valueof input char.
“\n” ?
![Page 57: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/57.jpg)
if statement
if( expression ){ statement 1; }else{ statement 2; }
TestYES
Statement 1
NO
IF
statement 3;Statement 2
Statement 3
else
![Page 58: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/58.jpg)
Exercise #2 Modify the previous program, such that
it counts the number of occurrences of ‘{’ and ‘}’ in a file
![Page 59: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/59.jpg)
Example: Word counting
#include <stdio.h>#define IN 1#defin OUT 2
/* count lines, word, characters */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);}
Word: separate by space,Tab, and newline
record whether nowis in a word or not
nl=(nw=(nc=0));OR
OR: ||AND: &&
Evaluate from left to right
![Page 60: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/60.jpg)
Outline
Variable and Arithmetic Expression The For Statement Symbolic Constants Character Input and Output Arrays Functions Arguments – Call by Value Character Arrays External Variables and Scope
![Page 61: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/61.jpg)
Example: Count digits Store occurrence of each digit in an
array
#include <stdio.h>
/* count digits, white space, and others */main(){ int c, i, nwhite, nother; int ndigit[10];
/* initialize */
Declaration of array
ndigit[0]ndigit[1]ndigit[2]ndigit[3]ndigit[4]
ndigit[9]??????
16 bits
![Page 62: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/62.jpg)
/* initialize */ nwhite = nother = 0; for(i=0; i<10; ++i) ndigit[i] = 0; while((c = getchar() != EOF) if(c >= '0' && c <= '9') ++ndigit[ c-'0' ]; else if(c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; printf("digit = "); for(i=0; i<10; ++i) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); }
in the interval [0,9]
![Page 63: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/63.jpg)
Multi-way decision
if( expression ){ statement 1; }else if { statement 2; }…else{ statement N;}
statement K;
TestYES
Statement 1
NO
IF
Statement K
TestYES
Statement 2
else if
else if…
Test
else
NO
Statement N
YESStatement N-1
…
![Page 64: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/64.jpg)
Outline
Variable and Arithmetic Expression The For Statement Symbolic Constants Character Input and Output Arrays Functions Arguments – Call by Value Character Arrays External Variables and Scope
![Page 65: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/65.jpg)
Functions Function, subroutine, procedure printf, getchar, putchar, …
input
output
…body…(hidden from user)
![Page 66: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/66.jpg)
Example: Power Power(2,3) -> 23
#include <stdio.h>
int power(int m, int n);
main(){ int i; for(i=0; i<10; ++i) printf("%d %d %d\n", i, power(2,i), power(-3,i)); return 0;}
function prototypec.f. function definitionint power(int, int);
![Page 67: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/67.jpg)
int power(int base, int n){ int i, p; p = 1; for(i=1; i<=n; ++i) p = p*base; return p;}
Return-type function-name(parameter declarations…){Declarationsstatements}
Samefile?
These variable names are localto this function
![Page 68: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/68.jpg)
Functions (cont.)
power(int base, int n)
return p;
…body…(hidden from user)
power(2,i)
arguments(formal arguments)
parameters(actual arguments)
![Page 69: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/69.jpg)
Early definition
int power();…int power(int, int)int base, n;{ int i, p; p = 1; for(i=1; i<=n; ++i) p = p*base; return p;}
? Compiler cannotcheck the correctnessof the parameters of function calls
![Page 70: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/70.jpg)
Outline
Variable and Arithmetic Expression The For Statement Symbolic Constants Character Input and Output Arrays Functions Arguments – Call by Value Character Arrays External Variables and Scope
![Page 71: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/71.jpg)
Call by value
Store-program concept Program is data
Data segment
Program segment
…
…
memory
Data segment
Program segment
main
power
…
address
![Page 72: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/72.jpg)
2i
Program segment
…
…
memory
Program segment
main
power
…
address
power(2,i)
basen
Call by value
![Page 73: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/73.jpg)
Call by value - example
int power(int base, int n){ int p;
for(p=1; n>0; --n) p = p * base; return p;}
int power(int base, int n){ int i, p; p = 1; for(i=1; i<=n; ++i) p = p*base; return p;}
![Page 74: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/74.jpg)
Outline
Variable and Arithmetic Expression The For Statement Symbolic Constants Character Input and Output Arrays Functions Arguments – Call by Value Character Arrays External Variables and Scope
![Page 75: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/75.jpg)
Character arrays Character arrays = strings
“Hello\n”
H e l l o \n \0
Null character
H e l l o \n \0s
結尾字元char s[10];
S[0]S[1]S[2]S[3] …
… …
S[9]
![Page 76: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/76.jpg)
Character arrays: example Read text lines and print the longest
while (there’s another line) if (it’s longer than the previous longest) save it save its lengthprint longest line
Algorithm outline
![Page 77: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/77.jpg)
#include <stdio.h>#define MAXLINE 1000
int getline(char line[], int maxline);void copy(char to[], char from[]);
main(){ int len; int max; char line[MAXLINE]; char longest[MAXLINE]; max = 0; while((len=getline(line, MAXLINE)) > 0) if(len > max){ max = len; copy(longest, line); } if(max > 0) printf("%s", longest); return 0;}
0 if no input
![Page 78: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/78.jpg)
int getline(char s[], int lim){ int c, i; for(i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c; if(c == '\n'){ s[i]=c; ++i; } s[i]='\0'; return i;}
void copy(char to[], char from[]){ int i i=0; while((to[i] = from[i]) != '\0') ++i;}
left to right evaluation
when i=0?
![Page 79: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/79.jpg)
lineMAXLINE
…
…
memory
Program segment
main
getline
…
address
slim
Call by valuefor char arrays
int getline(line, MAXLINE)…
…
char line[MAXLINE]
line[0]…line[MAXLINE-1]
![Page 80: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/80.jpg)
Outline
Variable and Arithmetic Expression The For Statement Symbolic Constants Character Input and Output Arrays Functions Arguments – Call by Value Character Arrays External Variables and Scope
![Page 81: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/81.jpg)
Scope of variables Local variables in a function
Automatic variables
int getline(char s[], int lim){ int c, i; for(i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c;
…}
code segment
Data segment
enter
Allocatewhen entering
![Page 82: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/82.jpg)
#include <stdio.h>#define MAXLINE 1000
int max;char line[MAXLINE];char longest[MAXLINE];
int getline(void);void copy(void);
main(){ int len; extern int max; extern char longest[]; max = 0; while((len=getline()) > 0) if(len > max){ max = len; copy(); } if(max > 0) printf("%s", longest); return 0;}
int getline(void){ int c, i; extern char line[]; for(i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) s[i] = c; if(c == '\n'){ s[i]=c; ++i; } s[i]='\0'; return i;}
void copy(void){ int i extern char line[], longest[]; i=0; while((to[i] = from[i]) != '\0') ++i;}
External var.definition
External var.declaration
![Page 83: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/83.jpg)
External variables Advantage
Less communication of variables in functions calls
Disadvantage Variables can be changed in unexpected
way Functions lose its generality (must live with
the external variables…)
![Page 84: The C programming language: Introduction Fall 2003, Jen-Chang Liu](https://reader036.vdocuments.site/reader036/viewer/2022062300/56649d385503460f94a1226b/html5/thumbnails/84.jpg)
External variables (cont.) Write them in a header file
extern int max;extern char line[];extern char longest[];
ggyy.h
#include <ggyy.h>
int getline(void){ int c, i;
for(i=0; …}