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 ...
随机推荐
- Sublime Text 3083破解/汉化
破解码:—– BEGIN LICENSE —– Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 6C0EEB94 BC99 ...
- java小程序
1.水仙花数 这个数等于它的每位数的立方和 (三位数) 示例一: int i; int j; int k; for (i=1;i<=9;i++) { for (j=0;j<=9;j++){ ...
- [ActionScript 3.0] 通过三角形获得 3D 效果
Flash Player 10 和更高版本,Adobe AIR 1.5 和更高版本 在 ActionScript 中,使用Graphics.drawTriangles()方法执行位图转换,因为 3D ...
- [ActionScript 3.0] AS3 绘制正四面体(线条)
package { import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; im ...
- 组播(Multicast)传输
组播(Multicast)传输: 在发送者和每一接收者之间实现点对多点网络连接. 如果一台发送者同时给多个的接收者传输相同的数据,也只需复制一份的相同数据包.它提高了数据传送效率.减少了骨干网络出现拥 ...
- Python访问私有变量
代码: class Counter(object): __secount=0 publicfs=0 def getcount(self): self.__secount+=1 self.publicf ...
- 解决删除/升级Python导致Ubuuntu无法进入桌面的问题
找到问题的原因后于是换个思路,想大概修复了python,Ubuntu进入桌面应该也就没啥问题了.于是重新安装Python发现还是无济于事.也通过/usr/bin/python: can't find ...
- JavaScriptResult用法
写MVC项目的时候,一开始就遇到返回脚本无法执行的情况,查阅博客园中老鸟的各种文章,最后没有得出个所以然,没办法,找项目经理实地讨论. public JavaScriptResult Hello() ...
- vim 显示当前文件名 缩进设置 常用设置
- Xcode自动注释插件
开源xcode插件:规范注释生成器VVDocumenter 1.类似eclipse 和 vs studio 在前面输入/// 后触发,自动生成代码注释,如图 2.GitHub工程文件地址:https: ...