第十六题

The following is a small C program split across files. What do you expect the output to be, when both of them compiled together and run?
File1.c
int arr[];
File2.c
extern int *arr;
int main()
{
arr[] = ;
return ;
}

题目讲解:

编译完运行发生段错。

File1.c中声明的是个数组,File2.c中声明的是个指针,虽然名字一样,但俩arr处于不同的内存地址,Flie2.c中的arr==NULL,对0地址操作是非法的。

将File2.c中的”extern int *arr;”改为”extern int arr[80];”就可以了。

第十七题

Explain the output of the following C program (No, the output is not ).
#include<stdio.h>
int main()
{
int a=;
switch(a)
{ int b=;
case : printf("b is %d\n",b);
break;
default:printf("b is %d\n",b);
break;
}
return ;
}

题目讲解:

输出:b is 0

switch判断后直接跳转到相应的case/default处,不会执行之前的赋值语句。

第十八题

What is the output of the following program? (Again, it is not , (if the size of integer is )).
#define SIZE 10
void size(int arr[SIZE])
{
printf("size of array is:%d\n",sizeof(arr));
} int main()
{
int arr[SIZE];
size(arr);
return ;
}

题目讲解:

数组做参数传递时退化为指针,“void size(int arr[SIZE]) ”等价于“void size(int *arr) ”。size(arr)给size函数传入的参数是指针,所以sizeof(arr)是指针的大小。

第十九题

The following is a simple c program, in which there is a function called Error to display errors. Can you see a potential problem with the way Error is defined?
#include <stdlib.h>
#include <stdio.h>
void Error(char* s)
{
printf(s);
return;
} int main()
{
int *p;
p = malloc(sizeof(int));
if(p == NULL)
{
Error("Could not allocate the memory\n");
Error("Quitting....\n");
exit();
}
else
{
/*some stuff to use p*/
}
return ;
}
题目讲解:
网上搜了下,没有统一的解释,说说个人的理解。
Google “void Error(char* s)”,发现如下几种打印字符串的方式:
方式1:
void error(char *msg)
{
fprintf(stderr, msg);
}
方式2:
void error(char *msg)
{
printf(msg);
fflush(stdout);
}
方式3:
void error(char *msg)
{
puts(msg);
}
根据第四题的解释我们知道,stdout是行缓冲,只有遇到’\n’,缓冲区的内容才会打印出来,stderr是无缓冲,写向stderr的内容
可以立马打印出来。所以我推断,题目中的Error函数的隐患是,若传进去的字符串不带’\n’,该错误消息就不会立马打印出来,
直到遇到’\n’,或人为fflush(stdout),或缓冲区溢出,或进程退出才会把缓冲区内的错误消息打印出来。

第二十题

What is the differnce between the following function calls to scanf?(Please notice the space carefully in the second call. Try removing it and observe the behaviour of the program)
#include <stdio.h>
int main()
{
char c;
scanf("%c",&c);
printf("%c\n",c); scanf(" %c",&c);
printf("%c\n",c); return ;
}
题目讲解:
当第二个scanf没有空白符时,运行代码,输入a,回车后,会打印a,换行,换行,然后程序结束,字符’a’被第一个scanf读取,字符’\n’被第二个scanf读取;
当第二个scanf有空白符时,运行代码,输入a,回车,输出a,若继续敲回车,程序不结束,直到输入了字符(非换行符)后,打印该字符,程序结束。
C99的7.19.6.2第五条说,包含空白符的指令读取输入中的第一个非空白字符。
A directive composed of white-space character(s) is executed by reading input up to 
the first non-white-space character (which remains unread), or until no more characters
can be read.

C puzzles详解【16-20题】的更多相关文章

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

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

  2. 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 ...

  3. C puzzles详解【6-8题】

    第六题 #include<stdio.h> int main() { ; switch(a) { ': printf("ONE\n"); break; ': print ...

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

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

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

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

  6. C puzzles详解【38-45题】

    第三十八题 What is the bug in the following program? #include <stdlib.h> #include <stdio.h> # ...

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

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

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

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

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

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

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

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

随机推荐

  1. Windows 64位下安装Redis详细教程

    方法/步骤 在D盘新建文件夹[redis],右键解压Redis ZIP包,把所有文件解压到redis文件夹中.(其他盘符也可以滴^_^) 文件介绍: redis-benchmark.exe       ...

  2. Yii 框架生成缩略图

    控制器 if($model->load(Yii::$app->request->post()))        { //原图            $model->img = ...

  3. block作为类的属性时用copy

    1. block作为类的属性时用copy Block属性的声明,首先需要用copy修饰符,因为只有copy后的Block才会在堆中,栈中的Block的生命周期是和栈绑定的 <栈 :由系统维护的局 ...

  4. Notepad++隐藏的用法

    Notepad++是一个非常强大的文本编辑器. 它里面有很多有用的插件. 我认为对我比较有帮助的一个是NppExporter.它可以将Notepad++中彩色的代码原样的复制到Word中. 想要获得彩 ...

  5. CSS设置图片垂直居中的方法

    如果是应用了表格,那么设置单元格为align="center"就可以使其中的一切内容居中.如果没有应用表格要想设置图片居中就有点困难了.困难来自不按"常规出牌" ...

  6. vb中&和+的区别

    在字符串连接时+号只能是两个字符串线连接&号可以是字符串与另一种类型的数据相连接.例如"a"+"b"是合法的,而 "a"+2是错误的 ...

  7. nginx 环境搭建(基于linux)

    Nginx是一种服务器软件,故而其最主要.最基本的功能当然是可以与服务器硬件结合,让程序员可以将程序放在Nginx服务器上,将程序发布出去,让成千上万的网民可以浏览.除此之外,Nginx是一种高性能的 ...

  8. weblogic92 :BEA-101020

    weblogic92 启动时后台出现错误:<BEA-101020> 解决方案: 1.删除临时文件: user_projects/domains/base_domain/servers/Ad ...

  9. 一个简单的AJAX实例

    创建一个简单的XMLHttpRequest,从一个TXT文件中返回数据. 来源于菜鸟教程 <!DOCTYPE html><html><head><meta c ...

  10. Flex 三态复选框

    在周末挤出了一点时间,写了一个三态复选框的组件,单独使用没有价值,不过集成到树之中可以很好的实现三态树,今天上午便把三态树组件也完成了,Flex自定义组件基本无所不能,此组件基于最新的Flex4.6( ...