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 ...
随机推荐
- web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" ...
- JS正则实例
<html> <body> </body> </html> <script> var strSrc = "xxa1b01c001y ...
- [AIR] 在 Adobe AIR 中为不同屏幕尺寸的多种设备提供支持
转自:http://www.adobe.com/cn/devnet/air/articles/multiple-screen-sizes.html 无论是改编原本在浏览器 Flash Player 中 ...
- 国内Lua先驱的Lua源码总结
http://www.codingnow.com/temp/readinglua.pdf <Lua源码欣赏> By 云风
- [datatable]C# DataTable 如何排序
DataTable dt = new DataTable(); dt.Columns.Add("ProductID", typeof(string)); dt.Columns.Ad ...
- java中的==和!=
java中一般很少用到==和!=,除了用于和null比较,如: if(null==o){ } //或者 if(null!=o){ } 其他地方比较一律用equals(); 建议:写完代码后在整个项目中 ...
- oracle分布式事务总结-转载
基本概念 Local Coordinator:在分布事务中,必须参考其它节点上的数据才能完成自己这部分操作的站点. Global Coordinator:分布事务的发起者,负责协调这个分布事务. Co ...
- SQL Server 2012 Enterprise Core Edition和SQL Server 2012 Enterprise Edition的区别
core没有图形界面,只有power shell界面,给没有图形界面的windows用的.
- cocos2dx lua 绑定之二:手动绑定自定义类中的函数
cococs2dx 3.13.1 + vs2013 + win10 1.首先按照<cocos2dx lua 绑定之一:自动绑定自定义类>绑定Student类 2.在Student类中增加一 ...
- 蓄水池采样算法(Reservoir Sampling)
蓄水池采样算法 问题描述分析 采样问题经常会被遇到,比如: 从 100000 份调查报告中抽取 1000 份进行统计. 从一本很厚的电话簿中抽取 1000 人进行姓氏统计. 从 Google 搜索 & ...