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 ...
随机推荐
- WebService中实现上传下载文件
不多说,直接看代码: /*上传文件的WebService*/ using System; using System.Collections; using System.Collections.Gene ...
- 九度OJ1081
这道题又一次更新了我的世界观与人生观Orz……最开始我是设计了一个O(n)的递推算法,本以为可以轻松AC没想到居然TLE了……然后搜了一下题解,才发现这道题要用矩阵的思想去做. 通过对题目的分析,我们 ...
- log4net 日志写入MongoDB 实现分布式日志
本人在.net framework 4.5下测试成功,首先需要安装log4mongo-net组件,见https://www.nuget.org/packages/log4mongo-net/ vs20 ...
- 立体匹配:关于OpenCV读写middlebury网站的给定的视差并恢复三维场景的代码
Middlebury是每个研究立体匹配算法的人不可能不使用的网站,Middlebury提供了许多标准的测试库,这极大地推进了立体匹配算法的进展.Middlebury提供的标准库,其计算出的视差保存在后 ...
- Oracle 数据类型映射C#
Oracle 数据类型映射 下表列出 Oracle 数据类型及其与 OracleDataReader 的映射. Oracle 数据类型 由 OracleDataReader.GetValue 返回的 ...
- MapReduce从HBase读写数据简单示例
就用单词计数这个例子,需要统计的单词存在HBase中的word表,MapReduce执行的时候从word表读取数据,统计结束后将结果写入到HBase的stat表中. 1.在eclipse中建立一个ha ...
- cwRsync 配置文件详解
GLOBAL PARAMETERS(全局参数) The first parameters in the file (before a [module] header) are the global p ...
- 剑指Offer:面试题16——反转链表(java实现)
问题描述 定义一个函数,输入一个链表的头结点,反转该链表并输出反转后的链表的头结点.链表结点如下: public class ListNode { int val; ListNode next = n ...
- Flex应用一览表
1.Flex控件之repeater和radioButton控件应用 2.Flex之DataGrid和Tree控件的数据源XML格式 3.Flex控件之combobox应用 4.转:Flex的Arra ...
- 【LeetCode】13. Roman to Integer 罗马数字转整数
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...