C puzzles详解【38-45题】
第三十八题
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=; i<SIZE; i++)
*(a + i) = i * i;
for (i=; i<SIZE; i++)
printf("%d\n", *a++);
free(a);
return ;
}
题目讲解:
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=, b = ; printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
printf(&a["WHAT%c%c%c %c%c %c !\n"], ["this"],
["beauty"],["tool"],["is"],["sensitive"],["CCCCCC"]);
return ;
}
知识点讲解:
对于数组中的元素,最常见的表示方法是:地址[偏移],如a[0],a[1],a[2]。
还有一种不常见的表示方法是:偏移[地址],如0[a],1[a],2[a]。
举例:
#include <stdio.h>
int main()
{
int a[] = {, , };
printf(“%d, %d, %d\n”, [a], [a], [a]);
return ;
}
运行结果为: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[];
printf("Enter a string:\n");
scanf("%[^a]",dummy);
printf("%s\n",dummy);
return ;
}
知识点讲解:
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 = ;
main.c
------
extern int a;
int main()
{
printf("a = %d\n",a);
return ;
}
Let's see what happens, when the files are compiled together:
bash$ gcc a.c b.c main.c
bash$ ./a.out
a =
Hmm!! 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题】的更多相关文章
- C puzzles详解【51-57题】
第五十一题 Write a C function which does the addition of two integers without using the '+' operator. You ...
- C puzzles详解【46-50题】
第四十六题 What does the following macro do? #define ROUNDUP(x,n) ((x+n-1)&(~(n-1))) 题目讲解: 参考:http:// ...
- C puzzles详解【34-37题】
第三十四题 The following times. But you can notice that, it doesn't work. #include <stdio.h> int ma ...
- C puzzles详解【31-33题】
第三十一题 The following is a simple C program to read and print an integer. But it is not working proper ...
- C puzzles详解【26-30题】
第二十六题(不会) The following is a simple program which implements a minimal version of banner command ava ...
- C puzzles详解【21-25题】
第二十一题 What is the potential problem with the following C program? #include <stdio.h> int main( ...
- C puzzles详解【16-20题】
第十六题 The following is a small C program split across files. What do you expect the output to be, whe ...
- C puzzles详解【13-15题】
第十三题 int CountBits(unsigned int x) { ; while(x) { count++; x = x&(x-); } return count; } 知识点讲解 位 ...
- 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 ...
随机推荐
- 解决mysql"Access denied for user'root'@'IP地址'"问题
在另一台服务器使用 MySQL-Front链接时: 解决方法: 在MySQL服务器上使用root登录后,执行如下SQL语句: mysql 登录命令: >mysql -u root -p; 然后执 ...
- linux 交换分区分配规则
一般地, 内存小于2G ,则swap=2*RAM, 内存大于2G, 则swap=2+RAM, 然后满足上述规则就行.
- 在备份和导入mysql数据库遇到的几个问题
一.怎么导出和备份 1.普通方法,运用工具或者命令直接导出sql脚本,以navicat为例,直接选中数据库,转储sql文件 问题:当有视图或者函数执行失败时不好处理 2.视图函数和表数据分开导出 以n ...
- sikuli运行出现问题:Win32Util.dll: Can't load 32-bit .dll on a AMD 64 bit platform
Win32Util.dll: Can't load 32-bit .dll on a AMD 64 bit platform 解决办法:将该工程设置成JDK为32位的 ,我设成1.6的32位不能用,因 ...
- poj 1459 Power Network : 最大网络流 dinic算法实现
点击打开链接 Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 20903 Accepted: ...
- Appium移动自动化测试(四)--先跑起来再说(第一个测试用例-手机YY)
说明 本文将详细说明如何使用Appnium完成:打开手机YY欢迎页面->按住屏幕向左滑动4次->按下"立即体验"按钮->按下"直播"按钮,的整 ...
- Debian的一个命令
dpkg是一个Debian的一个命令行工具,它可以用来安装.删除.构建和管理Debian的软件包.下面是它的一些命令解释:1)安装软件命令行:dpkg -i <.deb file name> ...
- MSP430F149学习之路——按键
代码一: /********************************** 程序功能:用按键控制LED灯熄灭 ***********************************/ #incl ...
- DNS协议 实践
根据DNS协议发送UDP请求,然后获取IP地址 头文件: #ifndef __DNS__ #define __DNS__ #include <stdio.h> #include <s ...
- Compilation failed: this version of PCRE is not compiled with PCRE_UTF8 support at offset 0
在安装pcre-8.13.tar.gz时候出了错,说是缺少libpcre.so.0 下面是解决方法.真不容易哦,一个问题来没解决,新问题就出来了.一环扣一环,会搞死去.. errorgrep: err ...