First C program remark of C and C

  • Slides: 50
Download presentation

/* First C program , remark of C and C++, many lines allowed */

/* First C program , remark of C and C++, many lines allowed */ #include <stdio. h> // for using printf() #include <stdlib. h> int main(int argc, char *argv ) // C++ 形式的單行註解。 { printf(“Hello World !n”) ; // ‘n’ : new line return (0); } 註解、縮排、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(“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); // 當成n(=2)位數 printf("|%-5 d|n", k); // 靠左排列

printf("1234567890n"); printf("%dn", k); printf("%2 dn", k); // 當成n(=2)位數 printf("|%-5 d|n", k); // 靠左排列 printf(“%05 dn”, k); // 前方空位補零 printf(“%+5 dn”, k); // 正數前給 + 號 printf("%4 Xn", k); // 以 16進位表示 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 if ( a>0 ) printf(“a>0n”); printf(“next statement”); Ex. if-else statement if

Ex. if statement if ( a>0 ) printf(“a>0n”); printf(“next statement”); Ex. if-else statement if ( a>0 ) printf(“a>0n”); else printf(“a<=0n”); printf(“next statement”); if (1) if (0. 5) if (-2) if (0) 非零值的條件表示成立或 true! a = 0; if ( a = 0 ) printf(“a=0”); if ( a == 0 ) printf(“a=0”); (邏輯相等是兩個等號!)

程式碼與對應的流程圖 no 是描述左圖的程式碼? a != 0 int a = -1; yes a>0 if (

程式碼與對應的流程圖 no 是描述左圖的程式碼? 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 ( score>=90 ) printf(“Grade A”); 對應展開上一 頁的if-else的 配對關係 if ( score>=90 ) printf(“Grade

if ( score>=90 ) printf(“Grade A”); 對應展開上一 頁的if-else的 配對關係 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”);

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”); 程式碼對齊: 將else 前移。

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

C/C++ 陣列的宣告與使用範例 (1) 陣列(array)的宣告: 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]=‘�’;

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);

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

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: (byte: 位元組)

位元的介紹 bit (binary digit):位元 => 0 or 1 byte = 8 bits: (byte: 位元組) word = 2 bytes = 16 bits; (word: 字組) 位 元 的 稱 呼 (與冪次有關) 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 (nibble: 4

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