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 ...
随机推荐
- tar.gz和rpm安装文件(转载)
from:http://bbs.chinaunix.net/thread-2277750-1-1.html Linux软件的二进制分发是指事先已经编译好二进制形式的软件包的发布形式,其优点是安装使用容 ...
- Win7允许被ping
我们可以通过命令行方式来执行入站 Ping 的规则是启用还是禁用,命令行如下: netsh firewall set icmpsetting 8 netsh firewall set icmpsett ...
- Dapper.NET使用(转)
Dapper.NET使用 本文目录 Dapper.NET使用 1.为什么选择Dapper 2.以Dapper(4.0)为例. 2.1 在数据库中建立几张表. 2.2实体类. 3.使用方法 3.1 一 ...
- Codeforces 716C[数论][构造]
/* CF傻逼构造题 某人要经过n回合游戏,初始分值是2,等级为1. 每次有两种操作 1.无条件,分值加上自己的等级数. 2.当目前的数字是完全平方数并且该数字开方以后是等级数加1的整数倍,那么可以将 ...
- 再战map
以前自己整理过map容器,但是好像没有这篇这么系统... Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处 ...
- UIButton 详解
1)创建 UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 风格有如下 typedef enum { UIButt ...
- 移植ok6410
tftp u-boot.bin http://blog.csdn.net/link_hui/article/details/5593518 LED driver http://blog.csdn.ne ...
- 一个关于发邮件的类,可以模拟发送对smtp服务器或者是本地文件夹
namespace SportsStore.Domain.Concrete { public class EmailSettings { public string MailToAddress = & ...
- 下载和使用 Open XML PowerTools
安装 Open XML SDK 2.5 首先,需要安装 Open XML SDK 2.5 ,从这个地址下载安装程序:http://www.microsoft.com/en-in/download/de ...
- Linux系统自启动脚本
只需编辑/etc/init.d/rc.local文件,在最后加上你的脚本即可.比如:我已经编写了一个脚本shell.sh,存放在/home/mars704/Desktop/ 下面在终端输入 gedit ...