第三十一题

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. CQL操作

    http://docs.datastax.com/en/cql/3.1/pdf/cql31.pdf CQL是Cassandra Query Language的缩写,目前作为Cassandra默认并且主 ...

  2. Python语言快速入门

    Python的主提示符(>>>):是解释器告诉你它正在等待你输入的下一个语句 Python的次提示符(...):告诉你解释器正在等待你输入当前语句的其他部分 [简介] Python( ...

  3. 深入研究java.lang.ThreadLocal类(转)

    引用:http://lavasoft.blog.51cto.com/62575/51926/ 一.概述   ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并 ...

  4. Jmeter-Maven-Plugin高级应用:Selecting Tests To Run

    地址:https://github.com/jmeter-maven-plugin/jmeter-maven-plugin/wiki/Advanced-Configuration Selecting ...

  5. SQL Server 2005中的分区表(三):将普通表转换成分区表(转)

    在设计数据库时,经常没有考虑到表分区的问题,往往在数据表承重的负担越来越重时,才会考虑到分区方式,这时,就涉及到如何将普通表转换成分区表的问题了. 那么,如何将一个普通表转换成一个分区表 呢?说到底, ...

  6. [Flex] ButtonBar系列——皮肤和外观设置

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  7. Java实现单向链表的增删改查

    class List<T> { private class Node { private T data; private Node next; private Node(T data) { ...

  8. 九度OJ1468

    这道题其实就是个很简单的静态链表,需要注意的是,地址一共有5位,最后输出的时候如果之前是使用int类型存储地址的话,一定要强制规定输出的位数(5位),否则有可能会将高位省略.(如地址00001输出为1 ...

  9. 光流算法:Brox光流的OpenCV源码解析

    OpenCV中DeepFlow代码其实是Brox光流,而非真正的DeepFlow光流,在将近一个月的研究.移植及优化过程中,对Brox光流有了较深刻的认识.我对OpenCV中源码进行了详细的分析,并以 ...

  10. NYOJ-228 士兵杀敌5

    士兵杀敌(五) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5 描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为0~M,每次有任务的时候,总会有一批编号连在一起人请战(编 ...