第三十一题

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题】的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. C puzzles详解【16-20题】

    第十六题 The following is a small C program split across files. What do you expect the output to be, whe ...

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

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

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

随机推荐

  1. RPC介绍

    转载http://blog.csdn.net/mindfloating/article/details/39474123/ 近几年的项目中,服务化和微服务化渐渐成为中大型分布式系统架构的主流方式,而 ...

  2. JMeter中3种参数值的传递

    小伙伴们在使用JMeter的过程中,肯定会遇到参数值传递的问题,下面来和大家总结下,在使用JMeter做压力测试的时候,常见的3种参数值的传递是怎样的. (一)从CSV文件读取要批量输入的变量 假如我 ...

  3. 使用NodeJS将XML解析成JSON及性能比较

    并不是所有的API都是以JSON格式返回的.我们有时侯不得不处理一些XML.幸运的是有一个NodeJS模块 xml2js 可以帮你做这件事.   比如,我们要处理下面这段XML   <?xml ...

  4. jQuery 中屏蔽浏览器的F5刷新功能

    //支持ie(6,7,8),火狐,谷歌,opera,等主流浏览器 $(document).keydown(function(e){ e=window.event||e; if(e.keyCode==1 ...

  5. hibernate one-to-many many-to-one 双向注解

    建表语句: DROP TABLE IF EXISTS `t_company`; CREATE TABLE `t_company` ( `companyId` ) unsigned NOT NULL A ...

  6. nyoj 84 阶乘的0

    点击打开链接 阶乘的0 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 计算n!的十进制表示最后有多少个0 输入 第一行输入一个整数N表示测试数据的组数(1<=N& ...

  7. 在Eclipse ee中成功使用jQuery UI插件

    关键代码截图如下:

  8. MongoDB条件查询

    1.查询集合中的所有记录 db.users.find() { "_id" : ObjectId("528b1173613e3289197a6486"), &qu ...

  9. 《Head First 设计模式》ch.3 装饰(Decorator)模式

    设计原则 类应该对修改关闭,对扩展开放(开放-关闭原则).在每个地方使用开放-关闭原则是一种浪费,也没有必要,因为这通常会引入新的抽象层次,增加代码复杂度.需要把注意力集中在设计中最有可能改变的地方. ...

  10. vc读写注册表

    注册表是Windows重要组成部分,注册表记录了大量有关电脑软硬件的信息.注册表中的值通过其名称标识.值名称由与键名相同的字符组成.值本身可以是字符串.二进制数据或者是32位无符号值.在这里我们主要运 ...