博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C puzzles详解【38-45题】
阅读量:4306 次
发布时间:2019-06-06

本文共 3457 字,大约阅读时间需要 11 分钟。

第三十八题

What is the bug in the following program?   #include 
#include
#define SIZE 15 int main() { int *a, i; a = malloc(SIZE*sizeof(int)); for (i=0; i
题目讲解:
printf打印栈中值时,a的值被改变,所以最后free(a)中a的值并不是之前分配的栈的起始地址。

第三十九题

Is the following a valid C program? If so, what is the output of it?   #include 
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 
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 
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?
知识点讲解:
参考博文:的评论。
此方法有如下局限:
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?)
题目讲解:
检测溢出的方法参考:

转载于:https://www.cnblogs.com/tanghuimin0713/p/3989397.html

你可能感兴趣的文章
从零开始的Docker ELK+Filebeat 6.4.0日志管理
查看>>
Sequelize的原始查询的时区问题
查看>>
How it works(1) winston3源码阅读(A)
查看>>
How it works(2) autocannon源码阅读(A)
查看>>
How it works(3) Tilestrata源码阅读(A)
查看>>
How it works(12) Tileserver-GL源码阅读(A) 服务的初始化
查看>>
uni-app 全局变量的几种实现方式
查看>>
echarts 为例讲解 uni-app 如何引用 npm 第三方库
查看>>
uni-app跨页面、跨组件通讯
查看>>
springmvc-helloworld(idea)
查看>>
JDK下载(百度网盘)
查看>>
idea用得溜,代码才能码得快
查看>>
一篇掌握python魔法方法详解
查看>>
数据结构和算法5-非线性-树
查看>>
数据结构和算法6-非线性-图
查看>>
数据结构和算法7-搜索
查看>>
数据结构和算法8-排序
查看>>
windows缺少dll解决办法
查看>>
JPA多条件动态查询
查看>>
JPA自定义sql
查看>>