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 0;
  }

题目讲解:

运行后发生段错。

问题出在这一行

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 5 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<<2 + a;
      return t;
  }

  int main()
  {
      int a = 1, b = 2,c = 3;
      PrintInt(FiveTimes(a));
      PrintInt(FiveTimes(b));
      PrintInt(FiveTimes(c));
      return 0;
  }

题目讲解:

函数FiveTimes中,

t = a<<2 + a;

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

t = (a<<2) + 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 = 10, b = 20;
      PrintInt(a);
      PrintInt(b);
      PrintInt(max(a,b));
  }

题目讲解:

编译有错误:

test.c: In function ‘max’
test.c:5: error: expected expression before ‘return’

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

改成

return (x > y) ? x : y;
时间: 2024-12-23 15:01:30

C puzzles详解【31-33题】的相关文章

C puzzles详解【26-30题】

第二十六题(不会) The following is a simple program which implements a minimal version of banner command available on most *nix systems. Find out the logic used in the program. #include<stdio.h> #include<ctype.h> char t[]={ 0,0,0,0,0,0,12,18,33,63, 33

C puzzles详解【13-15题】

第十三题 int CountBits(unsigned int x) { int count=0; while(x) { count++; x = x&(x-1); } return count; } 知识点讲解 位运算 关于位运算的一些例子参考: http://www.ugcs.caltech.edu/~wnoise/base2.html 题目讲解 x&(x-1)常见的两种应用: 1)计算x二进制形式中1的个数,每循环一次,将x二进制形式最右边的1变成0: 2)判断x是否是2的幂,若x&

C puzzles详解【51-57题】

第五十一题 Write a C function which does the addition of two integers without using the '+' operator. You can use only the bitwise operators.(Remember the good old method of implementing the full-adder circuit using the or, and, xor gates....) 题目讲解: 参考:ht

C puzzles详解【38-45题】

第三十八题 What is the bug in the following program? #include <stdlib.h> #include <stdio.h> #define SIZE 15 int main() { int *a, i; a = malloc(SIZE*sizeof(int)); for (i=0; i<SIZE; i++) *(a + i) = i * i; for (i=0; i<SIZE; i++) printf("%d\

C puzzles详解【46-50题】

第四十六题 What does the following macro do? #define ROUNDUP(x,n) ((x+n-1)&(~(n-1))) 题目讲解: 参考:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=814501 用于内存对齐,n为2的幂. 第四十七题 Most of the C programming books, give the following example for the definitio

C puzzles详解【34-37题】

第三十四题 The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work. #include <stdio.h> int main() { int i; int n = 20; for( i = 0; i < n; i-- ) printf("-"); return 0; }

C puzzles详解【16-20题】

第十六题 The following is a small C program split across files. What do you expect the output to be, when both of them compiled together and run? File1.c int arr[80]; File2.c extern int *arr; int main() { arr[1] = 100; return 0; } 题目讲解: 编译完运行发生段错. File1.

C puzzles详解【21-25题】

第二十一题 What is the potential problem with the following C program? #include <stdio.h> int main() { char str[80]; printf("Enter the string:"); scanf("%s",str); printf("You entered:%s\n",str); return 0; } 题目讲解: 易造成数组越界,sca

C puzzles详解【6-8题】

第六题 #include<stdio.h> int main() { int a=10; switch(a) { case '1': printf("ONE\n"); break; case '2': printf("TWO\n"); break; defa1ut: printf("NONE\n"); } return 0; } 题目讲解: “defalut”拼写错误. 注意a为int型,case后的’1’,’2’为char型. 第七