C puzzles详解【31-33题】
第三十一题
The following is a simple C program to read and print an integer. But it is not working properly. What is(are) the mistake(s)?
#include <stdio.h>
int main()
{
int n;
printf("Enter a number:\n");
scanf("%d\n",n); printf("You entered %d \n",n);
return ;
}
题目讲解:
运行后发生段错。
问题出在这一行
scanf("%d\n",n);
首先,scanf的第二个参数应该是个地址。若是从标准输入读入数据到变量n中,这一行应该改成
scanf("%d\n",&n);
再次编译运行,发现要输入一个数值后回车,程序并不会返回,再次输入一个数值后才会返回,n的值是第一次输入的值。
参考http://book.51cto.com/art/200901/106938.htm
‘\n’在scanf格式中不表示等待换行符,而是读取并放弃连续的空白字符,因此“%d\n”中的’\n’会让scanf读到非空白字符为止。要使用户输入一个数据回车后程序立马返回,去掉scanf中的’\n’即可。
即
scanf("%d",&n);
第三十二题
The following is a simple C program which tries to multiply an integer by using the bitwise operations. But it doesn't do so. Explain the reason for the wrong behaviour of the program.
#include <stdio.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int FiveTimes(int a)
{
int t;
t = a<< + a;
return t;
} int main()
{
int a = , b = ,c = ;
PrintInt(FiveTimes(a));
PrintInt(FiveTimes(b));
PrintInt(FiveTimes(c));
return ;
}
题目讲解:
函数FiveTimes中,
t = a<< + a;
‘+’的优先级高于’<<’,应改成
t = (a<<) + a;
第三十三题
Is the following a valid C program?
#include <stdio.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int max(int x, int y)
{
(x > y) ? return x : return y;
} int main()
{
int a = , b = ;
PrintInt(a);
PrintInt(b);
PrintInt(max(a,b));
}
题目讲解:
编译有错误:
test.c: In function ‘max’
test.c:: error: expected expression before ‘return’
将
(x > y) ? return x : return y;
改成
return (x > y) ? x : y;
C puzzles详解【31-33题】的更多相关文章
- 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详解【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 ...
随机推荐
- git 空提交和重置提交者(转载)
From:http://www.xiukun.me/git%E4%BD%BF%E7%94%A8-allow-empty-%E8%BF%9B%E8%A1%8C%E7%A9%BA%E7%99%BD%E6% ...
- 4个理由告诉你Java为何排行第一
Java已经有20年的历史了,甚至更久,而这取决于你所询问的人和你的计算方式.忽略它的年龄不看,Java依然排行第一.它的实用性.性能和向后兼容性都彰显其价值所在.2016年伊始,标志着我们已经走过了 ...
- .NET 中文转缩写拼音
public class CNToSpell { /// 汉字转拼音缩写 /// Code By MuseStudio@hotmail.com /// 2004-11-30 /// 要转换的汉字字符串 ...
- C++学习49 对二进制文件的读写操作
二进制文件不是以ASCII代码存放数据的,它将内存中数据存储形式不加转换地传送到磁盘文件,因此它又称为内存数据的映像文件.因为文件中的信息不是字符数据,而是字节中的二进制形式的信息,因此它又称为字节文 ...
- Mingyang.net:No identifier specified for entity
org.hibernate.AnnotationException: No identifier specified for entity: net.mingyang.modules.system.C ...
- 查看mysql表结构的几种方法
desc 表名; show columns from 表名; describe 表名; show create table 表名; use information_schemaselect * fro ...
- Codeforces 418d Big Problems for Organizers [树形dp][倍增lca]
题意: 给你一棵有n个节点的树,树的边权都是1. 有m次询问,每次询问输出树上所有节点离其较近结点距离的最大值. 思路: 1.首先是按照常规树形dp的思路维护一个子树节点中距离该点的最大值son_di ...
- 最大子序列和(O(n))
下面介绍一个线性的算法,这个算法是许多聪明算法的典型:运行时间是明显的,但是正确性则很不明显(不容易理解). //线性的算法O(N) long maxSubSum4(const vector<i ...
- 一个JS定时器类
学习js,因为函数和定时器直接的关系是非绑定的.我不能说我开启一个定时,然后拿一个变量去记录这个setInterval的返回值,这样很麻烦. 本着面向对象的思想,利用业余时间(周末在家里)写了一个类, ...
- android 提高进程优先级 拍照永不崩溃(闪退)
首先科普一下Android系统进程的优先级: 当系统的内存不足时, android系统将根据进程优先级选择杀死一些不太重要的进程. 进程优先级从高到低分别为: 1. 前台进程. 以下的进程为前台进程: ...