890 likes | 1.26k Vues
The C programming language: Introduction. Fall 2003, Jen-Chang Liu. Position of C. Human Natural language. High-level language. C, C++ Pascal,. applications. desktop. dos. shell. compiler. MS Windows. Unix Linux. Hardware machines. machine language. Introduction. B -> C
E N D
The C programming language:Introduction Fall 2003, Jen-Chang Liu
Position of C Human Natural language High-level language C, C++ Pascal,.. applications desktop dos shell compiler MS Windows Unix Linux Hardware machines machine language
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
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
Tutorial • Started by learning by examples… • Please try the examples on your own
如何產生可執行程式? Executable Code 可執行碼 C source Code C 原始程式 Compiler 編譯器 執行檔形式 *.exe 文字檔形式 *.c Turbo C 2.01 整合式編譯環境 Visual C++
Compiler 編譯器 • Unix: cc, gcc(GNU C compiler) • gcc has PC version, you may try to install it • Windows: Visual C++, Borland C++ aaa.c link aaa.obj compiler abc.exe bbb.c bbb.obj compiler …
Example 1 函式庫 Standard input/outputlibrary #include <stdio.h> main( ) { printf("Hello, world\n"); } printf scanf … functions Terminator of statement Body of main func arguments
C programming concept Turing machine state of the machines Control unit read/write head printf(“Hello world!”); … tape symbols
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 input output function
Example 1 (cont.) • “Hello, world\n” • Example 2 Character strings(string constant) Newline character \t: tab, \b: backspace, \\: \
Program components • Function • Contains statements that specify the computing operations to be done • Variable • Store values for computing
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
上課程式實做繳交規定program submission in class • 每次上課後將當天實做之程式繳交 • 方式: email • Subject: work日期 學號 • 例:work1027 93321001 • 只交上 .c 檔便可 • 繳交程式必須加上註解(comments),包括 • 姓名,學號 • 程式目的 • 每行的作用
Prog2-1 // 劉震昌, 93xx0xx // 程式目的:測試整數變數並印出 #include <stdio.h> void main(void) { int i; /* 變數宣告 declaration */ i=2; /* 設定變數 */ printf(“This is my %dnd C program”, i); // 印出訊息 }
變數宣告與設定 • 宣告:告知編譯器 data type與 變數名稱 • Ex. int i; • 變數設定 變數名稱可以任取,最好取有意義的文字 (第一個字不可為數字) 變數型態 i = 2; // 將 i 設成 2
Data types of Variables Integer with machine-dependent sizes int integer short short integer long long integer char character float floating point double double-precision case-sensitive keywords
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(double)); }
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); }
Variables and arithmetic expression (1.2) F C • C = (5/9)(F-32) 攝氏-華氏轉換 • Try to output the right table using printf 0 -17 20 -6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93 220 104 240 115 260 126 280 137 300 148
/* temperature version 0 */ #include <stdio.h> main() { printf(“0 -17\n"); printf(“20 -6\n"); printf(“40 4\n”); /* … */ } 0 -17 20 -6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93 220 104 240 115 260 126 280 137 300 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 -> C F+20 -> C F+20 -> C … 0 -17 20 -6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93 220 104 240 115 260 126 280 137 300 148
Arithmetic expression • C = 5*(F-32)/9; • C = 5/9*(F-32); Expressions are evaluated in this direction one-by-one =0 Because their type is integer, division will truncate the remainder
Equation in C • F = F + 20; • F 無解? F + 20 Temp F
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
More about printf • Format numeric output tight output Right justified %3d %d 0 20 40 60 120 0 20 40 60 120
Course break • http://alg.csie.ncnu.edu.tw/course/cprogram/introduction.htm
/* 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 -> C F+20 -> C F+20 -> C … 0 -17 20 -6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93 220 104 240 115 260 126 280 137 300 148
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); Evaluate order 整數位數 小數位數
Floating point (cont.) • Many print-out form • others %f %6f %.2f %6.2f %o otcal %x hexadecimal %c character %s character string %% %
流程控制:while loop 迴圈 Enter loop F=0 0 -17 20 -6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93 220 104 240 115 260 126 280 137 300 148 F=0 F=F+20 F=F+20 … F=F+20 Test condition F<=300 no exit Yes (non-0) Execute Loop body F=F+20
while loop 迴圈 Enter loop F=0 /* C Language */ Test condition F<=300 F = 0; while (F <= 300){ F = F+20; } no exit Yes(non-0) Execute Loop body F=F+20
#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 Declaration int = integer Variable assignment while loop
#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 point version of ex3. Floating point costant
Exercise 1 • Print the following numbers using while loops 1 1 2 3 5 8 13 21 34 55
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
for loop Enter loop F=0 initial value for(initial ; loop test ; increment){ Loop body… … } F<=300 Test condition no exit yes for(F=0; F<=300; F=F+20){ printf(“%d\n”, F); } Execute Loop body Execute Loop increment F=F+20;
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)); } exit loop test initial increment Type conversion
Exercise 2 • Print the following numbers using for loops 1 1 2 3 5 8 13 21 34 55
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"); } } * ** *** **** ***** ****** ******* ******** ********* **********
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"); }} * ** *** **** ***** ****** ******* ******** ********* **********
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; } } } * ** *** **** ***** ****** ******* ******** ********* **********
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; } * ** *** **** ***** ****** ******* ******** ********* **********
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; } } * ** *** **** ***** ****** ******* ******** ********* **********
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
Symbol constants Symbol name (Meaningful, easy to read) #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 value Replace this symbol name at compile time
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
Character I/O • A text stream is a sequence of characters getchar() getch() getche() getc() putchar(c) putch(c,stdout) putc(c,stdout) 輸入 stdin I/O devices are also taken as files 輸出 stdout
Example: File copying #include <stdio.h> /* echo, version 1 */ main() { int c; c=getchar(); while( c != EOF ){ putchar(c); c = getchar(); } } End Of File not equal to A constant defined in stdio.h NOT the same as any char values