第三十一题

  1. 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)?
  2. #include <stdio.h>
  3. int main()
  4. {
  5. int n;
  6. printf("Enter a number:\n");
  7. scanf("%d\n",n);
  8.  
  9. printf("You entered %d \n",n);
  10. return ;
  11. }

题目讲解:

运行后发生段错。

问题出在这一行

  1. scanf("%d\n",n);

首先,scanf的第二个参数应该是个地址。若是从标准输入读入数据到变量n中,这一行应该改成

  1. scanf("%d\n",&n);

再次编译运行,发现要输入一个数值后回车,程序并不会返回,再次输入一个数值后才会返回,n的值是第一次输入的值。

参考http://book.51cto.com/art/200901/106938.htm

‘\n’在scanf格式中不表示等待换行符,而是读取并放弃连续的空白字符,因此“%d\n”中的’\n’会让scanf读到非空白字符为止。要使用户输入一个数据回车后程序立马返回,去掉scanf中的’\n’即可。

  1. scanf("%d",&n);

第三十二题

  1. 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.
  2. #include <stdio.h>
  3. #define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
  4. int FiveTimes(int a)
  5. {
  6. int t;
  7. t = a<< + a;
  8. return t;
  9. }
  10.  
  11. int main()
  12. {
  13. int a = , b = ,c = ;
  14. PrintInt(FiveTimes(a));
  15. PrintInt(FiveTimes(b));
  16. PrintInt(FiveTimes(c));
  17. return ;
  18. }

题目讲解:

函数FiveTimes中,

  1. t = a<< + a;

‘+’的优先级高于’<<’,应改成

  1. t = a<<) + a;

第三十三题

  1. Is the following a valid C program?
  2. #include <stdio.h>
  3. #define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
  4. int max(int x, int y)
  5. {
  6. (x > y) ? return x : return y;
  7. }
  8.  
  9. int main()
  10. {
  11. int a = , b = ;
  12. PrintInt(a);
  13. PrintInt(b);
  14. PrintInt(max(a,b));
  15. }

题目讲解:

编译有错误:

  1. test.c: In function max
  2. test.c:: error: expected expression before return

  1. (x > y) ? return x : return y;

改成

  1. 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. IIS6.0中布署MVC站点(转)

    昨晚我写的API上线,API是vs2010 + MVC4开发的,需要布署到windows 2003 server + IIS6.0的环境中,之前一直是布在IIS7.0,比较熟悉, 换到IIS6.0,添 ...

  2. 找不到方法"Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)"的解决方案

    找不到方法"Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)" http://www.microsoft.com/down ...

  3. SQL 语句转换格式函数Cast、Convert 区别

    SQL 语句转换格式函数Cast.Convert CAST和CONVERT都经常被使用.特别提取出来作为一篇文章,方便查找. CAST.CONVERT都可以执行数据类型转换.在大部分情况下,两者执行同 ...

  4. SQL 获取各表记录数的最快方法

    select distinct o.name,i.rows from sysobjects o,sysindexes  i where o.id=i.id and o.Xtype= 'U' and i ...

  5. Words in Coding Theory

    Lemma d(x.y) wt(c,0) self-dual self-orthogonal even prime wt(C) matrix column permute permutation ge ...

  6. iOS 模态视图

    模态视图不是专门的某个类,而是通过视图控制器的presentViewController方法弹出的视图,我们称为模态视图. 模态视图出现的场景一般是临时弹出的窗口,譬如:登录窗口: 模态视图弹出时通过 ...

  7. python字典copy()方法

    python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...

  8. HtmlPrefixScopeExtensions

    http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

  9. CSS3 border-radius 属性和CSS outline 属性

    CSS3 border-radius 属性 border-radius 属性是一个简写属性,用于设置四个 border-*-radius 属性. 提示:该属性允许为元素添加圆角边框! 注释:按此顺序设 ...

  10. 代码生成器(CodeBuilder) 2 正式发布

    CodeBuilder是一个通过获取数据库表和字段定义,通过模板转换生成三层结构.实体模型等代码的工具. CodeBuilder第一版距今已过去4个年头了,第一版做的功能繁多,体积庞大,但是用起来不太 ...