//p30.c -- @CopyLeft by tsaiwn@cs.nctu.edu.tw //demo usage of char array ; using it as a string //請執行後看出"阿扁"的內碼 // Most computers adopt ASCII code + Big5 code // 香港中文碼用 HZ (漢字); 大陸中文碼用 GB (國標) // IBM 大電腦用 EBCDIC instead of ASCII code #include char haha[ ]={65, 66, 67, 164, 106, 165, 106, 165,107,0}; char gg[ ]="Hello李登輝heeheehee"; char*yy = "Hey 阿扁 How are you?\n"; // 注意這有 \n 換列 void print(unsigned char*); // 宣告 declare 說會有這函數 int main( ) { printf("haha=%s\n", haha); printf("gg is "); print(gg); // pass gg to print( ) printf("gg = %s\n", gg); printf("yy is "); print(yy); // pass yy to print( ) printf("yy = %s\nHit ENTER key..", yy); getchar( ); return 0; } /****** Running script .. D:\testc>gcc p35.c D:\testc>a haha=ABC大古右 gg is 72 101 108 108 111 167 245 181 110 189 247 104 101 101 104 101 101 104 101 101 0 gg = Hello李登輝heeheehee yy is 72 101 121 32 170 252 171 243 32 72 111 119 32 97 114 101 32 121 111 117 63 10 0 yy = Hey 阿扁 How are you? Hit ENTER key.. D:\testc> ***********************/ void print(unsigned char x[999]) { // Note that though parameter x[ ] is an array // actually 傳過來的是 pointer (指標) ! // 指標和 array (陣列) 用起來是互通的!! // 寫 x[999] 的 999 也是自欺欺人, 因無法知道 array 多大 // 實際上傳入此 function 的只是 array 第0個元素的 address int i=0; for( ; ; ) { // while(38==38) { printf("%d ", x[i]); if(x[i] == 0) break; // string is NULL-terminated ++i; if(i%5==0)printf(" "); if(i%10==0)printf("\n "); // 10 data/line } // for printf("\n"); // new line }// print( ///////////// END of the program /******** Running Script D:\test> D:\test> path C:\Dev-Cpp\bin;C:\TC\BIN;%path% D:\test> gcc p30.c D:\test> a.exe haha=ABC大古右 gg is 72 101 108 108 111 167 245 181 110 189 247 104 101 101 104 101 101 104 101 101 0 gg = Hello李登輝heeheehee yy is 72 101 121 32 170 252 171 243 32 72 111 119 32 97 114 101 32 121 111 117 63 10 0 yy = Hey 阿扁 How are you? Hit ENTER key.. D:\test> ***********************************/