標籤:
1.Linux 下第一支C程式,控制台列印一句話。
1 vi first.c //linux建立檔案 2 3 #include<stdio.h> 4 5 int main() { 6 printf("welcome to Linux ,this is the first C program!"); 7 return 0; 8 } 9 10 編譯;gcc -o first first.c //linux編譯檔案11 執行: ./first //linux執行檔案
2.第二隻C程式:瞭解C程式的結構,實現兩整數相加。
1 vi second.c 2 3 #include<stdio.h> 4 int main() { 5 int x , y , sum ; 6 x = 100 ; 7 y = 200 ; 8 sum = x + y ; 9 printf("sum is %d",sum);10 return 0;11 }12 //編譯執行命令與第一支C程式相同。
3.整形資料 占位元組數
1 #include<stdio.h> 2 3 int main() { 4 short int i; 5 int j; 6 long int k; 7 int a,b,c; 8 a = sizeof(i); 9 b = sizeof(j);10 c = sizeof(k); 11 12 printf("a is %d\n",a);13 printf("b is %d\n",b);14 printf("c is %d\n",c);15 16 //return 0; don‘t write return is OK?17 }18 output :19 a is 220 b is 421 c is 4
4.浮點型資料占位元組數,浮點數小數位元限制
1 #include<stdio.h> 2 3 int main(){ 4 float i ; 5 double j; 6 int a , b ; 7 a = sizeof(i); 8 b = sizeof(j); 9 printf("a is %d \n b is %d \n",a , b);// bit number10 11 12 float c = 88888.88888;13 double d = 88888888888.88888888;14 printf("c is %f \n d is %f \n",c,d);//%f小數最多輸出六位15 16 }17 18 output:19 a is 420 b is 821 c is 88888.890625 //i是單精確度浮點數,有效位元為7,整數佔據5位,小數佔2位,第二位位四捨五入結果,後面均為無效數字22 d is 88888888888.888885//j雙精確度,有效16位,整數佔11位,小數佔5位,後面為無效數。
5.字元型資料
1 //C語言字元用‘‘單引號:eg : ‘A‘ 2 //逸出字元:\n,換行,相當於enter 3 // \t,跳到下一個tab位置,相當於tab鍵 4 // \b,退格,將當前位置移到前一列,相當於backspace 5 // \\,反斜線字元 6 // \‘,單引號字元 7 // \",雙引號字元 8 // \0,Null 字元,用在字串中 9 // \ddd,一到三位8進位代表的字元,如\101代表字元A10 // \xhh,1到2位十六進位代表的字元,如\x41代表字元A11 //字元變數定義:char c1 , c2 = ‘A‘; 佔1位元組,8bit, ‘\n‘是一個逸出字元12 13 #include<stdio.h>14 15 int main(){16 int c1 ,c2 ; 17 char c3;18 printf("c3 is %d \n",sizeof(c3));19 20 c1 = ‘a‘ - ‘A‘;21 c2 = ‘b‘ - ‘B‘;22 c3 = ‘c‘ - 32;23 24 printf("c1 is %d and c2 is %d \n" , c1 ,c2);25 printf("c3 is %d and %c \n",c3,c3);26 return 0;27 }28 29 30 output:31 c3 is 132 c1 is 32 and c2 is 3233 c3 is 67 and C
Linux C 程式 (ONE)