C puzzles詳解【38-45題】

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   strong   for   

第三十八題

What is the bug in the following program?   #include <stdlib.h>  #include <stdio.h>  #define SIZE 15   int main()  {      int *a, i;      a = malloc(SIZE*sizeof(int));      for (i=0; i<SIZE; i++)          *(a + i) = i * i;      for (i=0; i<SIZE; i++)          printf("%d\n", *a++);      free(a);      return 0;  }
題目講解:
printf列印棧中值時,a的值被改變,所以最後free(a)中a的值並不是之前分配的棧的起始地址。

第三十九題

Is the following a valid C program? If so, what is the output of it?   #include <stdio.h>  int main()  {    int a=3, b = 5;    printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);    printf(&a["WHAT%c%c%c  %c%c  %c !\n"], 1["this"],       2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);    return 0;  }
知識點講解:
對於數組中的元素,最常見的表示方法是:地址[位移],如a[0],a[1],a[2]。
還有一種不常見的表示方法是:位移[地址],如0[a],1[a],2[a]。
舉例:
#include <stdio.h>int main(){int a[] = {0, 1, 2};printf(“%d, %d, %d\n”, 0[a], 1[a], 2[a]);return 0;}
運行結果為:0, 1, 2
printf(“%d, %d, %d\n”, 0[a], 1[a], 2[a]);
等效於
printf(“%d, %d, %d\n”, a[0], a[1], a[2]);

題目講解:
printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
等效於
printf("Hello! how is this? %s\n", "super");

printf(&a["WHAT%c%c%c  %c%c  %c !\n"], 1["this"],2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
等效於
printf("T%c%c%c  %c%c  %c !\n", ‘h’,‘a’,’t’,’i’,’s’,’C’);

第四十題

What is the output of the following, if the input provided is:Life is beautiful   #include <stdio.h>  int main()  {      char dummy[80];      printf("Enter a string:\n");      scanf("%[^a]",dummy);      printf("%s\n",dummy);      return 0;  }
知識點講解:
scanf格式控制符:
%[...]:讀取字串,直到遇到不是[]中的字元;
%[^...]:讀取字串,直到遇到[]中的字元。
如:
scanf("%[a-z]",dummy);輸入為”abc123”時,dummy為”abc”;
scanf("%[^a-z]",dummy);輸入為”123abc”時,dummy為”123”;
題目講解:
“%[^a]”表示讀取字串,直到遇到字元’a’。所以當輸入為”Life is beautiful”時,dummy為”Life is be”。
 

第四十一題

Note : This question has more to do with Linker than C language We have three files a.c, b.c and main.c respectively as follows: a.c--- int a;b.c--- int a = 10;main.c------ extern int a;int main(){        printf("a = %d\n",a);        return 0;}Let‘s see what happens, when the files are compiled together: bash$ gcc a.c b.c main.cbash$ ./a.outa = 10Hmm!! no compilation/linker error!!! Why is it so??
知識點講解:
參考《C陷阱與缺陷》第67頁 4.2 申明與定義。
若一個檔案中定義int a,另一個檔案中定義int a,編譯不會有問題,編譯器將a初始化為0;
若一個檔案中定義int a,另一個檔案中定義int a=10,a的值為10;
若一個檔案中定義int a=1,另一個檔案中定義int a=10,編譯會有重複定義的錯誤。

第四十二題

The following is the offset macros which is used many a times. Figure out what is it trying to do and what is the advantage of using it.   #define offsetof(a,b) ((int)(&(((a*)(0))->b)))
知識點講解:
計算結構體成員變數在結構體中的位移。a為結構體類型,b為成員變數。

第四十三題

The following is the macro implementation of the famous, Triple xor swap.   #define SWAP(a,b) ((a) ^= (b) ^= (a) ^= (b))What are the potential problems with the above macro?
知識點講解:
參考博文:http://www.cnblogs.com/tanghuimin0713/p/3220665.html的評論。
此方法有如下局限:
1)a和b不能是同一個變數,即如果執行SWAP(a, a)那麼不管原來a值是多少,執行後a值被置為0; 
2)a和b不能是浮點數,異或操作對浮點數沒有意義; 
3)a和b不能是結體體等複合資料型別,原因同上; 
4)a或b不能是運算式;

第四十四題

What is the use of the following macro?   #define DPRINTF(x) printf("%s:%d\n",#x,x)
題目講解:
列印x的值。
如a=10,DPRINTF(a)的結果為“a:10“。

第四十五題

Let‘s say you were asked to code a function IAddOverFlow which takes three parameters, pointer to an integer where the result is to be stored, and the two integers which needs to be added. It returns 0 if there is an overflow and 1 otherwise:   int IAddOverFlow(int* result,int a,int b)  {      /* ... */  }So, how do you code the above function? (To put in a nutshell, what is the logic you use for overflow detection?)
題目講解:
檢測溢出的方法參考:http://www.fefe.de/intof.html

C puzzles詳解【38-45題】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.