[教學]10小時學C 語言

  • Upload
    zhejian

  • View
    15.550

  • Download
    7

Embed Size (px)

Citation preview

10 C

Sep.22,1997 10 C [] 1997 6 3 page 2 4 10 13 21 32 40 46 55

C Turbo C C

1

C Turbo C

C C / Turbo C Turbo C Turbo C PATH PATH=C:\TC;C:\DOS;...... TC Turbo C PATH DOS TC Turbo C TC (Edit)(Compile)(Link)(Debug) (File)... CD C TC Del BS (C ) F3 F10 File Load Load File Name *.C * ? TC Enter F2 F10 File Save F10 File Write to New Name _ Enter Turbo C TC (pass 1) (pass 2)(.exe) PC Ctrl + F9 F10 Run Run TC TC TC TC Alt + F5 F10 Run User screen TC 2

Turbo C Alt + X F10 File Quit Turbo C TC Verify NONAME.C not saved. Save? (Y/N) Y N

3

C C hello.c 1#include 2main() 3{ 4 printf("Hello!"); 5} Hello! #include (prototype)(struct)(constant) C (declear) C INCLUDE\*.H #include INCLUDE\stdio.h stdio.h (standard I/O) printf() main() main() C int main() { } main() { } printf("Hello!"); C (;) 4

.h

#include #include

int main(int)

void main(void),

C (space)(tab) mainprintf C printf() PrintF() printf printf() TC printf printf Ctrl + F1 Help Help printf: formatted output to stdout int printf(const char *format, ...); Prototype in stdio.h Print formats a variable number of arguments according to the format, and sends the output to stdout. Returns the number of bytes output. In the event of error, it returns EOF. See also ecvt fprintf putc puts scanf vprintf TC Help printf stdout printf printf #include stdout byte EOF ecvt,fprintf,putc, puts,scanf,vprintf

TC Ctrl + F1 TC Help printf int printf(const char *format, ...); const

const char *format ...

format printf format C (") %d %d 10 () %d printf( "()" , );

arith.c 5

1#include 2void main(void) 3{ 4 printf("%d + %d = %d\n", 8 , 2 , 8+2 ); 5 printf("%d - %d = %d\n", 8 , 2 , 8-2 ); 6 printf("%d * %d = %d\n", 8 , 2 , 8*2 ); 7 printf("%d / %d = %d\n", 8 , 2 , 8/2 ); 8} 8 + 2 = 10 8 - 2 = 6 8 * 2 = 16 8 / 2 = 4 printf("%d + %d = %d\n", 8 , 2 , 8+2 ); () 3 %d 8 , 2 , 8+2 8 + 2 = 10 () \n \n 8 + 2 = 108 - 2 = 68 * 2 = 168 / 2 = 4 C ============ + * / ============ ============ ++ -% ============ ===================== ( ) ===================== ===================== 1 1 ===================== ============== 4 + 2 4 - 2 4 * 2 4 / 2 ============== ============== i++ ++i i-- --i 4 % 2 ============== ========== 6 2 8 2 ========== ========== i 1 i 1 0 ==========

C ** 2 ** 3 2 3 ^ 2 ^ 8 2 8 C C pow() pow( 2 , 3 ) 2 3 6

C ( ) C [ ] { } ( )C = = = = = ( 1 + 2 * ( 3 + 4 ) ) * 5 - 6 * 7 / ( 1 + 2 * ( 7 ) ) * 5 - 6 * 7 / ( 15 ) * 5 - 6 * 7 / 75 42 / 75 21 62 2 2 2 2 + + + + + 8 8 8 8 8

(Comments) Keyin () C /* */ C /* */ /* */ " C++ //.

comments.c or comments.cpp 1#include /* prototype : printf() */ 2void main(void) // main program 3{ 4/* */ 5/* printf("%d + %d = %d\n", 8 , 2 , 8+2 ); */ 6/* printf("%d - %d = %d\n", 8 , 2 , 8-2 ); 7 printf("%d * %d = %d\n", 8 , 2 , 8*2 ); 8 printf("%d / %d = %d\n", 8 , 2 , 8/2 ); // division 9*/ 10} // end of program

7

(Nested Comments) nestcom0.c 1#include /* prototype : printf() */ 2void main(void) 3{ 4/* */ 5/* 6 printf("%d + %d = %d\n", 8 , 2 , 8+2 ); 7/* printf("%d - %d = %d\n", 8 , 2 , 8-2 ); */ 8 printf("%d * %d = %d\n", 8 , 2 , 8*2 ); 9 printf("%d / %d = %d\n", 8 , 2 , 8/2 ); 10*/ 11} /* */ Compile 5 /* 10 */ 5 10 7 /* 7 */ 7 Turbo C 5 /* 7 */ 10 */ Compile F10 Options Compiler Source Nested comments Off Off On nestcom1.c printf(); nestcom2.c printf(); /* */ nestcom3.c /* */ nestcom1.c 1#include /* prototype : printf() */ 2void main(void) 3{ 4 /* */ 5 6 printf("%d + %d = %d\n", 8 , 2 , 8+2 ); /* 8 + 2 = 10 */ 7 printf("%d - %d = %d\n", 8 , 2 , 8-2 ); /* 8 - 2 = 6 */ 8 printf("%d * %d = %d\n", 8 , 2 , 8*2 ); /* 8 * 2 = 16 */ 9 printf("%d / %d = %d\n", 8 , 2 , 8/2 ); /* 8 / 2 = 4 */ 10 11}

8

nestcom2.c 1#include /* prototype : printf() */ 2void main(void) 3{ 4/* */ 5 6/* printf("%d + %d = %d\n", 8 , 2 , 8+2 ); */ /* 8 + 2 = 10 */ 7/* printf("%d - %d = %d\n", 8 , 2 , 8-2 ); */ /* 8 - 2 = 6 */ 8/* printf("%d * %d = %d\n", 8 , 2 , 8*2 ); */ /* 8 * 2 = 16 */ 9/* printf("%d / %d = %d\n", 8 , 2 , 8/2 ); */ /* 8 / 2 = 4 */ 10 11} nestcom3.c 1#include /* prototype : printf() */ 2void main(void) 3{ 4/* */ 5/* 6 printf("%d + %d = %d\n", 8 , 2 , 8+2 ); /* 8 + 2 = 10 */ 7 printf("%d - %d = %d\n", 8 , 2 , 8-2 ); /* 8 - 2 = 6 */ 8 printf("%d * %d = %d\n", 8 , 2 , 8*2 ); /* 8 * 2 = 16 */ 9 printf("%d / %d = %d\n", 8 , 2 , 8/2 ); /* 8 / 2 = 4 */ 10*/ 11}

9

C (constant)(variable) 0,1,2 1 10 type Char, C (A Z a z)( _ ) 0 9 32 TC if else switch default goto return char int void register short near typedef struct auto const interrupt sizeof _AX _AH _BX _BH _CX _CH _DX _DH _SI _DI 1000 32767 2147483647 (float) C 1 [, 2 [,...]] ; int long NumberOfStudent; MoneyInBank, interest;10

int, long, float, double

etc.

for case long signed far union static cdecl _AL _BL _CL _DL _BP

do break

while continue

float double unsigned huge enum volatile extern pascal asm _cs _ds _es _ss _SP _CS _DS _ES _SS

/* */ /* */

float RateOfInterest; char EndOfString; char OneStudentName[9];

/* */ /* */ /* */

(initial value) 1= 1 [, 2= 2 [,...]] ; int long float char char NumberOfStudent=60; MoneyInBank=1000000L; RateOfInterest=5.0; EndOfString='\0'; OneStudentName[9]=""; /* */ /* */ /* in % */ /* */ /* */

1000000 L 1000000 C TC TC TC L L char byte ' 'A' 'a' '0' '\101''\141''\60' '\0' '\x41''\x61''\x30''\x0'

'\0' "123""ABC""abc" 4 bytes strlen() ( '\0' ) int StringLen=strlen("123"); StringLen 3 strlen() #include = = (); C = C ==

11

PI = 3.1415926; r = 4; area = PI * r * r ; var.c #include void main(void) { int i,j; i = 10; j = 2; printf("%d + %d = %d\n", i , j , i+j ); printf("%d - %d = %d\n", i , j , i-j ); printf("%d * %d = %d\n", i , j , i*j ); printf("%d / %d = %d\n", i , j , i/j ); i = 20; j = 2; printf("%d + %d = %d\n", i , j , i+j ); printf("%d - %d = %d\n", i , j , i-j ); printf("%d * %d = %d\n", i , j , i*j ); printf("%d / %d = %d\n", i , j , i/j ); } 10 + 2 = 12 10 - 2 = 8 10 * 2 = 20 10 / 2 = 5 20 + 2 = 22 20 - 2 = 18 20 * 2 = 40 20 / 2 = 10 : (Global Variable): (Local Variable): (Static Variable):

12

printf() printf printf printf : printf("" , 1 , 2 , ... ); % \ % % % printf %c %d %ld %f %lf %Lf %s \ '\x41' A C Dec Hex \n 10 0x0A \t 9 0x09(1+8n) \b 8 0x08 \a 7 0x07 \r 13 0x0D \f 12 0x0C \\ 92 0x5C \ \' 39 0x27 ' \" 34 0x22 " \xHH 0xHH 0xHH *%% 37 0x25 % % 13

printf % %5d 5 %12ld 12 printf %8.3f 8 3 8 - ( 3 + 1 ) = 4 printf printf %-5d 5 5 (int)(long) % - + w d int % - + w ld long w (float)(double)(long double) % - + w . p f float % - + w . p lf double % - + w . p Lf long double p 6 w print.c 1#include 2void main(void) 3{ 4 printf("|%ld|\n", 123456 ); 5 printf("|%5ld|\n", 123456 ); 6 printf("|%d|\n", 123 ); 7 printf("|%5d|\n", 123 ); 8 printf("|%-5d|\n", 123 ); 14

9 printf("|%f|\n", 12.345 ); 10 printf("|%9f|\n", 12.345 ); 11 printf("|%9.2f|\n", 12.345 ); 12 printf("|%-9.2f|\n",12.345 ); 13} |123456| 123456 32767 , %ld |123456| 5 TC |123| | 123| 5 3 |123 | 5 3 |12.345000| TC 6 |12.345000| 9 | 12.35| 9 2 |12.35 | 9 2 scanf() C scanf keyboard scanf scanf("" , & 1 , & 2 , ... ); scanf printf scanf printf scanf %c %d %ld *%D %f %lf %Lf %s %F %D scanf() printf() scanf (pointer) i C &i &i i i () char c; /* */ /* c &c */ int i; /* */ /* i &i */ long l; /* */ /* l &l */ float f; /* */ /* f &f */ double d; /* */ /* d &d */ long double ld; /* */ /* ld &ld */ 15

char str[80]; /* () */ /* str[80] str */ int a[100]; /* */ /* a[100] a */ long b[100]; /* */ /* b[100] b */ float c[100]; /* */ /* c[100] c */ var.c scanf io.c 1#include 2void main(void) 3{ 4 int i,j; 5 6 printf("Enter 2 integers:"); 7 scanf("%d %d", &i, &j ); /* i j &i,&j */ 8 printf("Now, I find that ...\n"); 9 printf("%d + %d = %d\n", i , j , i+j ); 10 printf("%d - %d = %d\n", i , j , i-j ); 11 printf("%d * %d = %d\n", i , j , i*j ); 12 printf("%d / %d = %d\n", i , j , i/j ); 13} Enter 2 integers:20 4 i = 20 , j = 4 Now, I find that ... 20 + 4 = 24 20 - 4 = 16 20 * 4 = 80 20 / 4 = 5 scanf ((tab)) scanf i j Enter scanf scanf gets() gets gets atoi() atol() atof() ()() f2c.c 1#include 2void main(void) 3{ 4 int f,c; 5 6 printf("Enter the temperature in F : "); 16

7 scanf("%d", &f ); /* f &f */ 8 c = ( f - 32 ) * 5 / 9 ; /* */ 9 printf("%d degrees in F is %d degrees in C.", f, c); 10} Enter the temperature in F : 100 100 degrees in F is 37 degrees in C. ()() C = ( F - 32 ) * 5 / 9 F = C * 9 / 5 + 32 "" age.c 1#include 2void main(void) 3{ 4 float years, days; 5 6 printf("Enter the age of you : "); 7 scanf("%f", &years ); /* years &years */ 8 days = years * 365.25 ; /* */ 9 printf("You are %f days old.", days ); 10} Enter the age of you : 28.5 You are 10409.625000 days old. getche() scanf() Enter Enter getche() getche ch = getche(); getche() ch getche() ASCII code.c 1#include /* printf() */ 2#include /* getche() */ 3void main(void) 4{ 5 char ch; 6 17

7 ch = getche(); /* getche() */ 8 printf(" -- You typed %c.\n", ch ); 9 printf("Character %c has ASCII code %d.\n", ch, ch ); 10} A -- You typed A. Character A has ASCII code 65. ASCII 9 %c ch %d ch 10 Help Help printf: formatted output to stdout stdout int printf(const char *format, ...); printf Prototype in stdio.h #include Print formats a variable number of arguments according to the format, and sends the output to stdout. Returns the number of bytes output. stdout In the event of error, it returns EOF. byte EOF See also ecvt fprintf putc puts scanf vprintf Help Format Specifiers format % [flags] [width] [.prec] [F|N|h|l] type [ ] type Format of Output d signed decimal int d i signed decimal int i o unsigned octal int o u unsigned decimal int u x in printf = unsigned hexdecimal int x 16 () lowercase; in scanf = hexadecimal int scanf 16 X in printf = unsigned hexdecimal int X 16 () uppercase; in scanf = hexadecimal long scanf 16 f floating point [-]dddd.ddd f 314.159 e floating point [-]d.ddd e [+/-]ddd e 3.14159e2 g format e or f based on precision g f e E same as e except E for exponent E e E 18

G same as g except E for exponent G g E c single character c s print characters till '\0' or [.prec] s '\0' % the % character % % p pointer: near - YYYY; far - XXXX:YYYY p YYYY XXXX:YYYY n stores count of characters written so n far in the location pointed to by input argument [flag] What it Specifies [] none right-justify, pad 0 or blank to left 0 left-justify, pad spaces to right - + always begin with + or + blank print sign for negative values only # convert using alternate form: # c,s,d,i,u no effect c,s,d,i,u o 0 prepended to nonzero arg o 0 0 x or X 0x or 0X prepended to arg x,X 0x 0X e, E, f always use decimal point e,E,f g or G same as above but no g,G 0 trailing zeros [width] Effect on Output [] n at least n characters, blank-padded n 0n at least n characters, 0 left fill 0n 0 * next argument from list is width * [.prec] Effect on Output [.] none default precision .0 d,i,o,u,x default precision .0 d,i,o,u,x e, E, f no decimal point e,E,f .n at most n characters .n n * next argument from list is precision .* Modifier How arg is Interpreted F arg is far pointer F N arg is near pointer N h d,i,o,u,x,X arg is short int h l d,i,o,u,x,X arg is long int l l e, E, f, g, G arg is double l ( scanf) (scanf only) L e,E,f,g,G arg is long double L

19

Help scanf: performs formatted input from stdin stdin int scanf(const char *format, ...); scanf Prototype in stdio.h #include Returns the number of input fields processed successfully. It processes input according to the format and places the results in the memory locations pointed to by the arguments. See also atof cscanf fscanf getc printf sscanf vfscanf vscanf vsscanf Help getch: gets character from console, no echoing getche: gets character from the console and echoes to screen int getch(void); getch int getche(void); getche Prototype in conio.h #include Both functions return the character read. getch getche Characters are available immediately - no buffering of whole lines. Special keys such as function keys and arrow keys are represented by a two character sequence: a zero character followed by the 0 scan code for the key pressed. See also getpass cgets cscanf kbhit ungetch putch getchar getc

20

scanf ( Garbage In, Garbage Out ) C ( Flow Chart ) 6

___________ __________ __________ a, b, c __________ a, b, c a, b, c a,b,c

21

F = 100; C =(F-32)*5/9;

No Yes Yes No Yes No a>=b No Yes c=0; c=5; a b c=5; c=0;

a>=b No Yes b>=c No Yes c=1; a>=b No Yes 22

scanf("%d %d", &a,&b); a>=b No Yes

a b a b a b a b a b C > a>b a b < a= a>=b a b j ) max = i; 9 else max = j; 10 printf("The maximum of %d and %d is %d.\n", i, j, max ); 11} Enter 2 integer : 7 5 The maximum of 7 and 5 is 7. 25 if-else if if-else C else if-else else if-else 20 15 10 if-else if if( 1) { 1 } else if( 2) { 2 } else if( 3) { 3 } else if... else { n } ( 1) { 1 }; ( 2) { 2 }; ( 3) { 3 }; ... { n } No Yes 1 No Yes 2 No Yes ... 3 No Yes ... n 26 police.c 1#include /* printf(),scanf() */ 2void main(void) 3{ 4 int speed; 5 6 printf("Enter the speed = "); 7 scanf("%d", &speed ); 8 if( speed < 60 ) 9 printf("Too slow, speed up!\n"); 10 else if( speed < 90 ) 11 printf("Good day, boss.\n"); 12 else if( speed < 100 ) 13 printf("Too fast, slow down!\n"); 14 else 15 printf("I will give you a ticket!\n"); 16} Enter the speed = 50 Too slow, speed up! Enter the speed = 80 Good day, boss. Enter the speed = 95 Too fast, slow down! Enter the speed = 120 I will give you a ticket! 12 8 13 9 if( speed < 100 ) printf("Too fast, slow down!\n"); else if( speed < 90 ) printf("Good day, boss.\n"); else if( speed < 60 ) printf("Too slow, speed up!\n"); else printf("I will give you a ticket!\n"); speed < 100 Too fast, slow down! speed >= 100 I will give you a ticket! 27 No 60 60 No 90 Yes 90 No 100 60 Yes 100 Yes 60 90 90 100 100 calc.c 1#include /* printf(),scanf() */ 2void main(void) 3{ 4 float num1,num2; 5 char op; 6 7 for(;;) 8 { 9 printf("Enter number, operator, number\n"); 10 scanf("%f %c %f", &num1, &op, &num2); 11 if( op == '+' ) 12 printf("%f + %f = %f\n", num1, num2, num1+num2); 13 else if( op == '-' ) 14 printf("%f + %f = %f\n", num1, num2, num1-num2); 15 else if( op == '*' ) 16 printf("%f + %f = %f\n", num1, num2, num1*num2); 17 else if( op == '/' ) 2818 printf("%f + %f = %f\n", num1, num2, num1/num2); 19 } 20} Enter number, operator, number 4 + 8 4.000000 + 8.000000 = 12.000000 Enter number, operator, number 5 * 7 5.000000 + 7.000000 = 35.000000 ^C + - * / 7 Ctrl + Break Ctrl + C C && AND exp1 && exp2 exp1 exp2 || OR exp1 || exp2 exp1 exp2 exp1 exp2 ! NOT !exp1 exp1 exp1 ( sum = 5 + 3 ) 8 ( ( sum = 5 + 3 ) tcc yes2.c yes.exe yes2.exe 31 for C for for for for ( ; ; ) { } for for 1. 2. { } 3. { } 2 for for for ( i = 0 ; i < 100 ; i = i+1 ) printf("i = %3d\n", i ); for ( i = 0 , j = 0 ; i < 100 ; i = i+1 , j = j+2 ) printf("i = %3d j = %3d\n", i, j ); 32 for for ( ; ; ) { } for 1. 2. { } 3. { } 2 for for for ( ; ; ) { } for for { } (delay) ( ; ) for ( ; ) ASCII ascii.c 331#include /* printf() */ 2void main(void) 3{ 4 int i; 5 6 for( i = 32 ; i < 256 ; i = i+1 ) 7 printf("%3d=%c\t", i, i ); 8} 32= 33=! 34=" 35=# 36=$ 37=% 38=& 39=' ... 42=* 43=+ 44=, 45=46=. 47=/ 48=0 49=1 ... 52=4 53=5 54=6 55=7 56=8 57=9 58=: 59=; ... ... ... 252=* 253=* 254=* 255= for( i = 32 ; i < 256 ; i = i+1 ) ======== ======== ========= C i = i + 1 i++ i-- i = i - 1 1 i += 2 i = i + 2 i -= 2 i = i - 2 += -= i *= 2 i = i * 2 i /= 2 i = i / 2 n! n!.c 1#include /* printf(),scanf() */ 2void main(void) 3{ 4 long fact; 5 int n; 6 7 printf("Enter the value of n to compute n! : "); 8 scanf("%d", &n ); 9 printf("%d! = %d", n, n ); 10 fact = n; 11 for( n = n-1 ; n >0 ; n-- ) 12 { 13 fact *= n; 14 printf("x%d", n); 15 } 16 printf(" = %ld", fact); 17} Enter the value of n to compute n! : 5 n! = 5x4x3x2x1 = 120 n n! = n * (n-1) * (n-2) * ... * 2 * 1 fact n 5 9 n 5 10 fact = n; fact = 5 n n-1 4 n n-1 34 13 fact *= n fact = fact * n n 1 fact n fact n 5 5 5! = 5 n 5 4 5! = 5 1 5*4 4 5! = 5x4 2 5*4*3 3 5! = 5x4x3 3 5*4*3*2 2 5! = 5x4x3x2 4 5*4*3*2*1 1 5! = 5x4x3x2x1 5*4*3*2*1 1 5! = 5x4x3x2x1 = 120 for for for for (nest) ; ... for( ... ; ... ; ... ) { ; ... for( ... ; ... ; ... ) { ; ... } ; ... } if for(...;...;...) { for(...;...;...) { ... } ... for(...;...;...) {35 ... } } for(...;...;...) { for(...;...;...) { ... for(...;...;...) { ... } } ... } 99.c 1#include /* printf() */ 2void main(void) 3{ 4 int i, j; 5 6 for( i=1 ; i