C puzzles详解【21-25题】
第二十一题
What is the potential problem with the following C program?
#include <stdio.h>
int main()
{
char str[];
printf("Enter the string:");
scanf("%s",str);
printf("You entered:%s\n",str); return ;
}
题目讲解:
易造成数组越界,scanf改成如下形式比较保险
scanf("%79s",str);
不管输入多少个字节,最多只读取79个字节。
第二十二题
What is the output of the following program?
#include <stdio.h>
int main()
{
int i;
i = ;
printf("i : %d\n",i);
printf("sizeof(i++) is: %d\n",sizeof(i++));
printf("i : %d\n",i);
return ;
}
题目讲解:
输出为:
i: 10
sizeof(i++) is:4
i: 10
sizeof(i++),等效于sizeof(int)。sizeof的值在编译时决定,不会执行i++。
sizeof的更多讲解见第一题。
第二十三题
Why does the following program give a warning? (Please remember that sending a normal pointer to a function requiring const pointer does not give any warning)
#include <stdio.h>
void foo(const char **p) { }
int main(int argc, char **argv)
{
foo(argv);
return ;
}
题目讲解
- 关键字const
用const修饰变量,指定该变量为只读。 const int a = ;//变量a只读 const int *p;//p指向的int型数只读,p可以被修改 int *const p;//p为只读,p指向的int型数可以被修改
- 带const的一级指针和二级指针赋值
int a = ; const int *p; p = &a;//一级const指针赋值时,可以不将右边的指针转换为const型
int *p; const int **pp; pp = (const int **)&p;//二级const指针赋值时,必须将右侧二级指针转换为const型
- 带const的一级指针和二级指针传参
void foo(const char *p) {}
int main()
{
char *p;
foo(p);//p不用转化为const型
return ;
}
void foo(const char **pp) {}
int main()
{
char **pp;
foo((const char **)pp);//pp必须转化为const型
return ;
}
第二十四题
What is the output of the following program?
#include <stdio.h>
int main()
{
int i;
i = ,,;
printf("i:%d\n",i);
return ;
}
题目讲解:
同第十题。
输出: i:1
逗号运算符在所有的运算符中优先级最低,i = 1,2,3等效于(i = 1),2,3
若将”i = 1,2,3”改成”i = (1,2,3)”,i的值为3。
第二十五题(Reverse Polish Notation)
The following is a piece of code which implements the reverse Polish Calculator. There is a(are) serious(s) bug in the code. Find it(them) out!!! Assume that the function getop returns the appropriate return values for operands, opcodes, EOF etc..
#include <stdio.h>
#include <stdlib.h> #define MAX 80
#define NUMBER '0' int getop(char[]);
void push(double);
double pop(void);
int main()
{
int type;
char s[MAX]; while((type = getop(s)) != EOF)
{
switch(type)
{
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
push(pop() - pop());
break;
case '/':
push(pop() / pop());
break;
/* ...
* ...
* ...
*/
}
}
}
题目讲解:(不确定)
减法的减数和被减数反了,除法的除数和被除数反了。
C puzzles详解【21-25题】的更多相关文章
- 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详解【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 ...
随机推荐
- 解决Xshell和vim中文乱码(转载)
From:http://blog.csdn.net/lovey599/article/details/7275403 一般而言,乱码多是由于编码问题引起 的,在windows系统中,大多数情况下中文编 ...
- 使用k-近邻算法改进约会网站的配对效果
---恢复内容开始--- < Machine Learning 机器学习实战>的确是一本学习python,掌握数据相关技能的,不可多得的好书!! 最近邻算法源码如下,给有需要的入门者学习, ...
- iphone的手势与触摸编程学习笔记
一.基本概念与理解:Cocoa Touch将触摸事件发送到正在处理的视图.触摸传达的信息包括: 触摸发生的位置 (当前位置.最近的历史位置) 触摸的阶段 (按下.移动.弹起) 轻击数量 (tapCou ...
- 读取缓存模拟----FIFO
本例是模拟缓存的存储和读取. 存储:使用一个Map来存放,key是文件名,值为缓存对象 读取:返回相应的key对应的缓存.(如果缓存被修改,就重新加载缓存,再从缓存Map中读取相应缓存) 测试类:每2 ...
- js中使用s(c)标签
在js或者jquery中使用s标签,其实并不难理解,s标签也只是一个标签而已,当你想象成js+s标签=js+html标签就理解了 例如: <script type="text/java ...
- Oracle中对表的操作
表的创建与管理 创建表: CREATE TABLE TABLE_NAME ( COLUMN_NAME TYPE [DEFAULT VALUE], COLUMN_NAME TYPE [DEFAULT V ...
- [kuangbin带你飞]专题十四 数论基础
ID Origin Title 111 / 423 Problem A LightOJ 1370 Bi-shoe and Phi-shoe 21 / 74 Problem B ...
- iOS 数据存储 - 归档和解归档
这里的归档主要是用于自定义类的归档和解档.我们这里使用NSKeyedArchiver和NSKeyedUnarchiver来归档和解档. 注意:自己定义的类需要实现<NSCoding>,如: ...
- Android的消息处理机制,handler,message,looper(一)
当应用程序启动时,Android首先会开启一个主线程(也就是UI线程),主线程为管理界面中的UI控件.在程序开发时,对于比较耗时的操作,通常会为其开辟一个单独的线程来执行,以尽可能减少用户的等待时间. ...
- 在linux中安装和卸载mysql
[安装] 已经获取到linux版本的mysql安装包,包括mysql的server(服务端)和client(客户端)的安装包,假设安装包为: MySQL-server-5.0.22-0.i386.rp ...