一些覆蓋C語言許多知識點的題目,第一階段的課程從3月8日到現在已經進行了六周,
感覺自己不會的東西越來越多,面對計算機的時間也越來越長,眼睛現在已經有點模糊了,
希望自己能對得起自己的這份努力,加油,加油,加油!
____________________________________________________________
1.下面的程序輸出結果是多少?
#includeint main() { int x=2,y,z; x *=(y = z = 5);printf("%d\n”,x); //10 後面表達式的結果是5 z = 3; x ==(y = z); printf("%d\n”,x); //10 只是判斷,沒有給x賦值 x =(y == z); printf("%d\n”,x); //1 x = (y&z); printf("%d\n”,x); //3 位與操作 x = (y&&z); printf("%d\n”,x); //1 y = 4; x = (y | z); printf("%d\n”,x); //7 位或操作 x = (y || z); printf("%d\n”,x); //1 return 0; }
#includemain() { int b = 3; int arr[] = {6,7,8,9,10}; int *ptr = arr; *(ptr++)+=123; // arr[0]變成129,ptr指向arr[1] printf("%d,%d\n",*ptr,*(++ptr)); }
#includeint main() { unsigned char a = 0xa5; //a中裝著一個16進制數 二進制為:1010 0101 unsigned char b = ~a>>4; //a參與運算,自動轉換為int,b的結果二進制為 //1111 0101 printf("a = %d\n",a); //a的十進制為165 printf("b = %d\n",b); //b的十進制為245 return 0; }
int main() { unsigned int a = 0xCCCCCCF7; unsigned char i = (unsigned char)a; //i中裝著a的後兩位,也就是F7 char* b = (char*)&a; //b指向a printf("%08x,%08x",i,*b); //16進制輸出,000000F7 ,FFFFFFF7 return 0; }
(x & (x-1) == 0) ? printf("yes\n") : printf("yes\n")
#includemain() { int count = 0; int m = 9999; while(m) { count++; m = m&(m-1); } printf("%d\n",count); //8,進兩次循環m的二進制去掉一位 }
a = a + b; b = a - b; a = a - b; //還有更快的位操作版本: a = a ^ b; b = b ^ a; a = a ^ b;
#define S (365*24*60*60)
#define min((a), (b)) ((a)<(b))?(a):(b)
#includestruct{ short a1; short a2; short a3; }A; struct{ long a1; short a2; }B; int main() { char* ss1 = "0123456789"; char ss2[] = "0123456789"; char ss3[100] = "0123456789"; int ss4[100]; char q1[] = "abc"; char q2[] = "a\n"; char* q3 = "a\n"; char (*str1)[100] = (char(*)[100])malloc(100); void* str2 = (void*)malloc(100); printf("%d\n",sizeof(ss1)); //4 printf("%d\n",sizeof(ss2)); //11 printf("%d\n",sizeof(ss3)); //100 printf("%d\n",sizeof(ss4)); //400 printf("%d\n",sizeof(q1)); //4 printf("%d\n",sizeof(q2)); //3 printf("%d\n",sizeof(q3)); //4 printf("%d\n",sizeof(A)); //6 結構體都跟其中最大的的元素類型對齊 printf("%d\n",sizeof(B)); //16 跟long對齊,8*2 printf("%d\n",sizeof(*str1)); //100 printf("%d\n",sizeof(str2)); //4 return 0; }
#includemain() { char *a []={"hello","the","world"}; char **pa=a; pa++; printf(*pa); //the }
#includeint main() { int x=10, y=10, i; for(i=0;x>8;y=i++) { printf("%d,%d",x--,y); //10, 10 return 0; } }
typedef struct s { int i; int * p; }S; main() { S s; //定義一個結構體,其中有兩個元素,i 和 *p int *p = &s.i; //定義指針變量p,裝著s中i的地址 p[0] = 4; //下標運算,在i中放入4 p[1] = 3; //在p中放入3 s.p = p; //把指針p中的地址,也就是s中i的地址給了s中的p s.p[1] = 1; //下標運算,把s中的p的東西,也就是i的地址的下一位,也就是 //s中p裝入一個1 s.p[0] = 2; //在這一行崩潰,p中裝著一個常量,無法進行寫操作 }