第三十八题

  1. What is the bug in the following program?
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #define SIZE 15
  5. int main()
  6. {
  7. int *a, i;
  8.  
  9. a = malloc(SIZE*sizeof(int));
  10.  
  11. for (i=; i<SIZE; i++)
  12. *(a + i) = i * i;
  13. for (i=; i<SIZE; i++)
  14. printf("%d\n", *a++);
  15. free(a);
  16. return ;
  17. }
  1. 题目讲解:
  1. printf打印栈中值时,a的值被改变,所以最后free(a)中a的值并不是之前分配的栈的起始地址。

第三十九题

  1. Is the following a valid C program? If so, what is the output of it?
  2. #include <stdio.h>
  3. int main()
  4. {
  5. int a=, b = ;
  6.  
  7. printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
  8. printf(&a["WHAT%c%c%c %c%c %c !\n"], ["this"],
  9. ["beauty"],["tool"],["is"],["sensitive"],["CCCCCC"]);
  10. return ;
  11. }
  1. 知识点讲解:
  1. 对于数组中的元素,最常见的表示方法是:地址[偏移],如a[0],a[1],a[2]。
    还有一种不常见的表示方法是:偏移[地址],如0[a],1[a],2[a]。
  1. 举例:
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int a[] = {, , };
  5. printf(“%d, %d, %d\n”, [a], [a], [a]);
  6. return ;
  7. }
  1. 运行结果为: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]);
  2.  
  3. 题目讲解:
    printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
    等效于
    printf("Hello! how is this? %s\n", "super");
  4.  
  5. printf(&a["WHAT%c%c%c  %c%c  %c !\n"], 1["this"],
  6. 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’);

第四十题

  1. What is the output of the following, if the input provided is:
  2. Life is beautiful
  3. #include <stdio.h>
  4. int main()
  5. {
  6. char dummy[];
  7. printf("Enter a string:\n");
  8. scanf("%[^a]",dummy);
  9. printf("%s\n",dummy);
  10. return ;
  11. }
  1. 知识点讲解:
  1. scanf格式控制符:
  1. %[...]:读取字符串,直到遇到不是[]中的字符;
  1. %[^...]:读取字符串,直到遇到[]中的字符。
  1. 如:
  1. scanf("%[a-z]",dummy);输入为”abc123”时,dummy为”abc”;
  1. scanf("%[^a-z]",dummy);输入为”123abc”时,dummy为”123”;
  1. 题目讲解:
  1. “%[^a]”表示读取字符串,直到遇到字符’a’。所以当输入为”Life is beautiful”时,dummy为”Life is be”。
 

第四十一题

  1. Note : This question has more to do with Linker than C language
  2. We have three files a.c, b.c and main.c respectively as follows:
  3. a.c
  4. ---
  5. int a;
  6. b.c
  7. ---
  8. int a = ;
  9. main.c
  10. ------
  11. extern int a;
  12. int main()
  13. {
  14. printf("a = %d\n",a);
  15. return ;
  16. }
  17. Let's see what happens, when the files are compiled together:
  18. bash$ gcc a.c b.c main.c
  19. bash$ ./a.out
  20. a =
  21. Hmm!! no compilation/linker error!!! Why is it so??
  1. 知识点讲解:
  1. 参考《C陷阱与缺陷》第67 4.2 申明与定义。
  1. 若一个文件中定义int a,另一个文件中定义int a,编译不会有问题,编译器将a初始化为0
  1. 若一个文件中定义int a,另一个文件中定义int a=10a的值为10
  1. 若一个文件中定义int a=1,另一个文件中定义int a=10,编译会有重复定义的错误。

第四十二题

  1. 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.
  2. #define offsetof(a,b) ((int)(&(((a*)(0))->b)))
  1. 知识点讲解:
  1. 计算结构体成员变量在结构体中的偏移。a为结构体类型,b为成员变量。

第四十三题

  1. The following is the macro implementation of the famous, Triple xor swap.
  2. #define SWAP(a,b) ((a) ^= (b) ^= (a) ^= (b))
  3. What are the potential problems with the above macro?
  1. 知识点讲解:
  1. 参考博文:http://www.cnblogs.com/tanghuimin0713/p/3220665.html的评论。
  1. 此方法有如下局限:
  1. 1ab不能是同一个变量,即如果执行SWAP(a, a)那么不管原来a值是多少,执行后a值被置为0
  1. 2ab不能是浮点数,异或操作对浮点数没有意义;
  1. 3ab不能是结体体等复合数据类型,原因同上;
  1. 4ab不能是表达式;

第四十四题

  1. What is the use of the following macro?
  2. #define DPRINTF(x) printf("%s:%d\n",#x,x)
  1. 题目讲解:
  1. 打印x的值。
  1. a=10DPRINTF(a)的结果为“a:10“。

第四十五题

  1. 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:
  2. int IAddOverFlow(int* result,int a,int b)
  3. {
  4. /* ... */
  5. }
  6. So, how do you code the above function? (To put in a nutshell, what is the logic you use for overflow detection?)
  1. 题目讲解:
  1. 检测溢出的方法参考:http://www.fefe.de/intof.html

C puzzles详解【38-45题】的更多相关文章

  1. C puzzles详解【51-57题】

    第五十一题 Write a C function which does the addition of two integers without using the '+' operator. You ...

  2. C puzzles详解【46-50题】

    第四十六题 What does the following macro do? #define ROUNDUP(x,n) ((x+n-1)&(~(n-1))) 题目讲解: 参考:http:// ...

  3. C puzzles详解【34-37题】

    第三十四题 The following times. But you can notice that, it doesn't work. #include <stdio.h> int ma ...

  4. C puzzles详解【31-33题】

    第三十一题 The following is a simple C program to read and print an integer. But it is not working proper ...

  5. C puzzles详解【26-30题】

    第二十六题(不会) The following is a simple program which implements a minimal version of banner command ava ...

  6. C puzzles详解【21-25题】

    第二十一题 What is the potential problem with the following C program? #include <stdio.h> int main( ...

  7. C puzzles详解【16-20题】

    第十六题 The following is a small C program split across files. What do you expect the output to be, whe ...

  8. C puzzles详解【13-15题】

    第十三题 int CountBits(unsigned int x) { ; while(x) { count++; x = x&(x-); } return count; } 知识点讲解 位 ...

  9. C puzzles详解【9-12题】

    第九题 #include <stdio.h> int main() { float f=0.0f; int i; ;i<;i++) f = f + 0.1f; if(f == 1.0 ...

随机推荐

  1. Java多线程之新类库中的构件CountDownLatch

    使用CountDownLatch类 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); ...

  2. 30天轻松学习javaweb_Range实现断点续传

    package com.wzh.test.http; import java.io.FileOutputStream; import java.io.IOException; import java. ...

  3. Bugtags 那些事儿

  4. 解决Ubuntu不能挂载ntfs格式硬盘

    以前都是自动可以挂在NTFS硬盘的,可是现在不能挂载了.搜了一下 这里有办法 http://forum.ubuntu.org.cn/viewtopic.php?t=313930 例如你是 Error ...

  5. C++学习13 类class和结构体struct的区别

    C++保留了C语言的 struct,并且加以扩充.在C语言中,struct 只能包含数据成员,不能包含成员函数.而在C++中,struct 类似于 class,既可以包含数据成员,又可以包含成员函数. ...

  6. C++学习8 构造函数的参数初始化表

    构造函数是一种特殊的成员函数,在创建对象时自动执行,主要用来进行初始化工作,例如对 private 属性的成员变量赋值. 对成员变量的初始化,除了在构造函数的函数体中一一赋值,还可以采用参数初始化表. ...

  7. JDBC中的批量插入和乱码解决

    字符集-乱码问题 用JDBC访问MySql数据库的时候,如果JDBC使用的字符集和MySql使用的字符集不一致,那么会导致乱码发生.解决办法当时是在使用JDBC的时候指定和数据库一样的字符集.我们可以 ...

  8. Microsoft Visual C++ 2015 Redistributable(x64) - 14.0.2306 设置失败

    想要在Windows 2008 R2 中 安装PHP, 需要安装 Microsoft Visual C++ 2015 Redistributable(x64) ,结果提供设置失败. 先中找到以下文字, ...

  9. POJ 3264-Balanced Lineup, NYOJ 119-士兵杀敌3 线段树

    士兵杀敌(三) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5 描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进行比 ...

  10. Remove Duplicates from Sorted List(链表)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...