First C program remark of C and C

  • Slides: 47
Download presentation

/* First C program , remark of C and C++*/ #include <stdio. h> //

/* First C program , remark of C and C++*/ #include <stdio. h> // for using printf() #define PI 3. 141592653 void main( ) // Program starts from here ! Remark of C++ { printf(“Hello World !n”) ; // ‘n’ : new line } 註解、縮排、include、define、main()、分號、控制碼、 Free-style 、{複合敘述}、void、< >與“ ”的區別

各種資料型態的儲值範圍(各系統可能不同) type modifier data type bytes char 1 range -128 ~ 127 unsigned char

各種資料型態的儲值範圍(各系統可能不同) type modifier data type bytes char 1 range -128 ~ 127 unsigned char int 1 4 0 ~ 255 -2147483648 ~ 2147483647 short long unsigned short unsigned long int int int float double 2 4 4 8 -32768 ~ 32767 -2147483648 ~ 2147483647 0 ~ 4294967295 0 ~ 65535 0 ~ 4294967295 3. 4 E-38 ~ 3. 4 E+38 1. 7 E-308 ~ 1. 7 E+308 *可使用 sizeof ( ) 來求出資料型態在記憶體中的儲存長度(bytes)。

#include "stdio. h" #include "conio. h" printf()的使用 void main() { char ch=128; //注意其值 float

#include "stdio. h" #include "conio. h" printf()的使用 void main() { char ch=128; //注意其值 float f=123. 456; int k=123; printf("testing. . n"); printf("sizeof( 2 )=%d => int defaultn", sizeof(2)); printf("sizeof(1. 0)=%d => double defaultn", sizeof(1. 0)); printf("\x 5 c"42%%45x 25n"); printf("1234567890n"); printf("%d %cn", ch); printf("%u %un", ch, (unsigned char) ch);

 printf("1234567890n"); printf("%dn", k); printf("%2 dn", k); printf("|%-5 d|n", k); printf("%05 dn", k); printf("%+5

printf("1234567890n"); printf("%dn", k); printf("%2 dn", k); printf("|%-5 d|n", k); printf("%05 dn", k); printf("%+5 dn", k); printf("%4 Xn", k); printf("1234567890n"); printf("%fn", f); printf("%5. 3 fn", f); printf("|%-8. 2 f|n", f); printf("%08. 3 fn", f); printf("%+8. 3 fn", f); printf("%4. 0 fn", f); }

scanf (“讀取資料的格式” [, &變數名稱] ) int k; scanf(“%d”, &k); float ft; scanf(“%f”, &ft); double

scanf (“讀取資料的格式” [, &變數名稱] ) int k; scanf(“%d”, &k); float ft; scanf(“%f”, &ft); double d; scanf(“%lf”, &d); char msg[80]; (msg : pointer) scanf(“%s”, &msg); scanf(“%s”, msg); // int hex, oct; scanf(“%d: %d”, &hex, &oct); 可不加& (separated by ‘: ’, it can be other character) int hex, oct; scanf(“%x”, &hex); (hexadecimal) scanf(“%o”, &oct); (octal)

Ex. if statement Ex. if-else statement if ( a>0 ) printf(“a>0n”); printf(“next statement”); else

Ex. if statement Ex. if-else statement if ( a>0 ) printf(“a>0n”); printf(“next statement”); else printf(“a<=0n”); printf(“next statement”);

no Ex. a!=0 int a=-1; yes a>0 if ( a!=0 ) if ( a>0

no Ex. a!=0 int a=-1; yes a>0 if ( a!=0 ) if ( a>0 ) printf(“a>0n”); else printf(“a=0n”); no printf(“a=0n”) What is the output ? a=0 敘述next yes printf(“a>0n”)

if- else-if………else 條件 1 yes 敘述 1 no 條件 2 no yes 條件n-1 no

if- else-if………else 條件 1 yes 敘述 1 no 條件 2 no yes 條件n-1 no yes 敘述 2 敘述n-1 敘述next 敘述n

Transfer score to grade if ( score>=90 ) printf(“Grade A”); else if ( score>=80

Transfer score to grade if ( score>=90 ) printf(“Grade A”); else if ( score>=80 ) printf(“Grade B”); else if ( score>=70 ) printf(“Grade C”); else if ( score>=60 ) printf(“Grade D”) else printf(“Grade E”);

if ( score>=90 ) printf(“Grade A”); else if ( score>=80 ) 對應展開上一 頁的if-else的配 對關係

if ( score>=90 ) printf(“Grade A”); else if ( score>=80 ) 對應展開上一 頁的if-else的配 對關係 printf(“Grade B”); else if ( score>=70 ) printf(“Grade C”); else if ( score>=60 ) printf(“Grade D”) else printf(“Grade E”);

C/C++ 陣列的宣告與使用範例 (1) 陣列的宣告: Data. Type Ex. int array. Name[number_of_element]; ary. Value[10]; float my.

C/C++ 陣列的宣告與使用範例 (1) 陣列的宣告: Data. Type Ex. int array. Name[number_of_element]; ary. Value[10]; float my. Array[30]; 現在我們有了: ary. Value[0], ary. Value[1], ary. Value[2], ary. Value[3], ary. Value[4], ary. Value[5], ary. Value[6], ary. Value[7], ary. Value[8], ary. Value[9]; 1) Zero-based: index starting from zero. 2) To obtain the size of the array, sizeof(ary. Name)/sizeof(ary. Name(0)) => 10

C/C++ 陣列的宣告與使用範例 (3) 隨機產生十個浮點數,並找出最大的數。 Ex. #include <stdlib. h> #define random(n) // for rand (rand()

C/C++ 陣列的宣告與使用範例 (3) 隨機產生十個浮點數,並找出最大的數。 Ex. #include <stdlib. h> #define random(n) // for rand (rand() % (n)) // 0 <= random(n) < n, n is an interger. . . int value[10], k, max; value[0] = random(101); max = value[0]; for (k=1; k<10; ++k) { value[k] = random(101); if ( max < value[k] ) max = value; } for (k=0; k<10; ++k) printf(“value[%d]=%dn”, k+1, value[k]); printf(“最大值是:%dn”, max);

C/C++ 陣列的宣告與使用範例 (6) Ex. 字元可視為數值或ASCII符號。 int k; char string[10], ch; ch=‘A’; string[9]=‘�’; // or

C/C++ 陣列的宣告與使用範例 (6) Ex. 字元可視為數值或ASCII符號。 int k; char string[10], ch; ch=‘A’; string[9]=‘’; // or string[9]=0; for (k=0; k<9; ++k) { string[k]=ch++; printf(“%c => %dn”, ch); } printf(“%sn”, string); printf(“%sn”, string+3);

do-while-loop 至少做一次 do { …………. 迴圈內容; }; while ( 條件式 ) ; 敘述next; 條件式

do-while-loop 至少做一次 do { …………. 迴圈內容; }; while ( 條件式 ) ; 敘述next; 條件式 no yes do { job; => } 敘述next while (cond); job; while (cond) { job; } // job; 至少做一次 do { printf(“Input your password: ”); scanf(“%s”, psword); } while ( check(psword) ); 敘述next; 檢查輸入的密碼

Ex. switch-case without break value=‘A’; switch (value) { case ‘A’ : printf(“A”); value=‘A’ ABCnot

Ex. switch-case without break value=‘A’; switch (value) { case ‘A’ : printf(“A”); value=‘A’ ABCnot ABC case ‘B’ : printf(“B”); value=‘B’ BCnot ABC case ‘C’ : printf(“C”); value=‘C’ Cnot ABC default : printf(“not ABC”); value=‘P’ not ABC }; 敘述next;

Ex. switch-case with break value=‘A’; switch (value) { case ‘A’ : printf(“A”); value=‘A’ A

Ex. switch-case with break value=‘A’; switch (value) { case ‘A’ : printf(“A”); value=‘A’ A case ‘B’ : printf(“B”); value=‘B’ BC case ‘C’ : printf(“C”); value=‘C’ C value=‘P’ not ABC break; default : printf(“not ABC”); }; 敘述next;

break : 跳出迴圈, 至敘述next(迴圈後的下一敘述) continue: 跳至條件變換或條件判斷處 break可用於for-loop, while, do-while, switch continue 則用於for-loop, while, do-while

break : 跳出迴圈, 至敘述next(迴圈後的下一敘述) continue: 跳至條件變換或條件判斷處 break可用於for-loop, while, do-while, switch continue 則用於for-loop, while, do-while for (k=1; k<10; ++ k) { ………. …. . break; ………. …. . continue; ……. . . }; while ( a>=1 ) { ………. …. . break; ………. …. . continue; ……. . . }; next-statement;

 // break case: sum=0; for (n=1; n<=10; ++n) { if ( n==5 )

// break case: sum=0; for (n=1; n<=10; ++n) { if ( n==5 ) break; sum+=n; sum=1+2+3+4=10 } // continue case: sum=0; for (n=1; n<=10; ++n) { if ( n==5) continue; sum+=n; } sum=1+2+3+4+6+7+8+9+10=50

位元的介紹 bit (binary digit):位元 => 0 or 1 byte = 8 bits:位元組 word =

位元的介紹 bit (binary digit):位元 => 0 or 1 byte = 8 bits:位元組 word = 2 bytes = 16 bits:字組 位 元 的 稱 呼 (與冪次有關) 1 0 1 0 1 27 26 25 24 23 22 21 20 bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0

位元的運算例子 (2/2) 若 A = 0 x. D 3 = (1101 0011)2 c) 將

位元的運算例子 (2/2) 若 A = 0 x. D 3 = (1101 0011)2 c) 將 high nibble反相 1 1 0 0 1 1 利用^ (xor)運算 A = A ^ 0 x. F 0; 1 1 0 0 d) 保留 low nibble : 1 1 0 0 1 1 利用& (and)運算 A = A & 0 x 0 F; 0 0 1 1