C puzzles详解【16-20题】
第十六题
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题】的更多相关文章
- 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 ...
- C puzzles详解【6-8题】
第六题 #include<stdio.h> int main() { ; switch(a) { ': printf("ONE\n"); break; ': print ...
- 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详解【38-45题】
第三十八题 What is the bug in the following program? #include <stdlib.h> #include <stdio.h> # ...
- 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( ...
随机推荐
- google protobuf 简单实例
1.定义proto文件: User.proto package netty; option java_package="myprotobuf"; option java_outer ...
- Oracle中增加,修改,删除表中的列
有些时候,当一个表已经建好,并且已经使用后,发现需要对表结构进行修改,这个时候就要对表中的列进行增删查改操作. 为表增加新列: ALTER TABLE table_name ADD ( column_ ...
- sql server 作业导出放到另外一台机器执行时报错的解决方法
SQL Server2008脚本创建作业失败,提示: 引用内容消息 515,级别 16,状态 2,过程 sp_add_job,第 137 行不能将值 NULL 插入列 'owner_sid',表 'm ...
- 光流算法:关于OpenCV读写middlebury网站给定的光流的代码
Middlebury是每个研究光流算法的人不可能不使用的网站,Middlebury提供了许多标准的测试库,这极大地推进了光流算法的进展.Middlebury提供的标准库,其计算出的光流保存在后缀名为. ...
- tarjan 边双连通分量 对点进行分组 每组点都在一个双连通分量里边
int dfn[N],low[N],id[N],s[N],p,num,t,son[N];//dfn记录dfs时间戳//low代表当前点到达的最小时间戳,id对点进行分组编号.num是时间戳//s临时存 ...
- 页面设计--CheckBoxList
CheckBoxList 下拉多选控件 控件属性如下图: 取值设置:多选只能从数据库表中来获取:支持过滤条件设置(支持权限条件值.控件值条件.系统变量值等做为过滤条件) web效果显示:
- 【转】关系映射文件***.hbm.xml详解
http://blog.sina.com.cn/s/blog_7ffb8dd5010144yo.html 附.Oracle使用标准.可变长度的内部格式来存储数字.这个内部格式精度可以高达38位. NU ...
- Java8新特性【转】
地址:http://ifeve.com/java-8-features-tutorial/ 1.简介 毫无疑问,Java 8是自Java 5(2004年)发布以来Java语言最大的一次版本升级,Ja ...
- Hive分布式的数据仓库
1.hive介绍 hive是一个数据仓库的解决方案,它的数据存储依赖于HDFS,数据结算依赖于MR,也就是说,hive就是一个在数据仓库的服务,它只需要安装到一台普通的PC上即可,仅仅对外提供SQL服 ...
- Memcached、Redis和MongoDB的区别
Memcached和Redis都是内存数据库. Memcached是多线程运行的: Redis单线程是单线程运行的: MongoDB是文档型的非关系型数据库..Net:RavenDB.