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中声明的是个数组,File2.c中声明的是个指针,虽然名字一样,但俩arr处于不同的内存地址,Flie2.c中的arr==NULL,对0地址操作是非法的。

将File2.c中的”extern int *arr;”改为”extern int arr[80];”就可以了。

第十七题

Explain the output of the following C program (No, the output is not 20).
  #include<stdio.h>
  int main()
  {
      int a=1;
      switch(a)
      {   int b=20;
          case 1: printf("b is %d\n",b);
                  break;
          default:printf("b is %d\n",b);
                  break;
      }
      return 0;
  }

题目讲解:

输出:b is 0

switch判断后直接跳转到相应的case/default处,不会执行之前的赋值语句。

第十八题

What is the output of the following program? (Again, it is not 40, (if the size of integer is 4)).
  #define SIZE 10
  void size(int arr[SIZE])
  {
          printf("size of array is:%d\n",sizeof(arr));
  }

  int main()
  {
          int arr[SIZE];
          size(arr);
          return 0;
  }

题目讲解:

数组做参数传递时退化为指针,“void size(int arr[SIZE]) ”等价于“void size(int *arr) ”。size(arr)给size函数传入的参数是指针,所以sizeof(arr)是指针的大小。

第十九题

The following is a simple c program, in which there is a function called Error to display errors. Can you see a potential problem with the way Error is defined?
  #include <stdlib.h>
  #include <stdio.h>
  void Error(char* s)
  {
      printf(s);
      return;
  }

  int main()
  {
      int *p;
      p = malloc(sizeof(int));
      if(p == NULL)
      {
          Error("Could not allocate the memory\n");
          Error("Quitting....\n");
          exit(1);
      }
      else
      {
          /*some stuff to use p*/
      }
      return 0;
  }
题目讲解:
网上搜了下,没有统一的解释,说说个人的理解。
Google “void Error(char* s)”,发现如下几种打印字符串的方式:
方式1:
void error(char *msg)
{
    fprintf(stderr, msg);
}
方式2:
void error(char *msg)
{
    printf(msg);
    fflush(stdout);
}
方式3:
void error(char *msg)
{
puts(msg);
}
根据第四题的解释我们知道,stdout是行缓冲,只有遇到’\n’,缓冲区的内容才会打印出来,stderr是无缓冲,写向stderr的内容可以立马打印出来。所以我推断,题目中的Error函数的隐患是,若传进去的字符串不带’\n’,该错误消息就不会立马打印出来,直到遇到’\n’,或人为fflush(stdout),或缓冲区溢出,或进程退出才会把缓冲区内的错误消息打印出来。

第二十题

What is the differnce between the following function calls to scanf?(Please notice the space carefully in the second call. Try removing it and observe the behaviour of the program)
  #include <stdio.h>
  int main()
  {
      char c;
      scanf("%c",&c);
      printf("%c\n",c);

      scanf(" %c",&c);
      printf("%c\n",c);

      return 0;
  }
题目讲解:
当第二个scanf没有空白符时,运行代码,输入a,回车后,会打印a,换行,换行,然后程序结束,字符’a’被第一个scanf读取,字符’\n’被第二个scanf读取;
当第二个scanf有空白符时,运行代码,输入a,回车,输出a,若继续敲回车,程序不结束,直到输入了字符(非换行符)后,打印该字符,程序结束。
C99的7.19.6.2第五条说,包含空白符的指令读取输入中的第一个非空白字符。
A directive composed of white-space character(s) is executed by reading input up to the ?rst non-white-space character (which remains unread), or until no more characters can be read.
时间: 2024-12-28 13:56:00

C puzzles详解【16-20题】的相关文章

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详解【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(&quo

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详解【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详解【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型. 第七

C puzzles详解【9-12题】

第九题 #include <stdio.h> int main() { float f=0.0f; int i; for(i=0;i<10;i++) f = f + 0.1f; if(f == 1.0f) printf("f is 1.0 \n"); else printf("f is NOT 1.0\n"); return 0; } 知识点讲解: 浮点寄存器 浮点寄存器是FPU的组成部分.硬件架构不同,浮点寄存器的个数和位数也不同.X86架构的浮

C puzzles详解【1-5题】

第一题 #include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int array[] = {23,34,12,17,204,99,16}; int main() { int d; for(d=-1;d <= (TOTAL_ELEMENTS-2);d++) printf("%d\n",array[d+1]); return 0; } 知识点讲解: 有关sizeof: siz

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\