C puzzles and my solution

题目地址:http://www.gowrikumar.com/c/

Dear visitor,

Thanks for your interest in C programming. In this page, you will find a list of interesting C programming questions/puzzles, These programs listed are the ones which I have received as e-mail forwards from my friends, a few I read in some books, a few from the internet, and a few from my coding experiences in C.

Most of the programs are meant to be compiled, run and to be explained for their behaviour. The puzzles/questions can be broadly put into the following categories:

  • General typo errors, which C programmers do often and are very difficult to trace.
  • Small programs which are extremely hard to understand at the first examination. These questions make a good excercise of reading and understanding effecient code written by others.

I have used Gnu/Linux/gcc for all of them. The order in which the programs appear doesn't have any relation with the level of difficulty. Please feel free to contact me if you need any help in solving the problems. My contact info. is available here And you might be interested in a few references for C programming, which I personally found very interesting.

If you are preparing for campus interviews, you might find the following link interesting:
http://placementsindia.blogspot.com

Regards,
Gowri Kumar

C puzzles

 


The expected output of the following C program is to print the elements in the array. But when actually run, it doesn't do so.

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

Find out what's going wrong.
hint

 

solution:sizeof 运算符返回的实际是unsigned int 类型,for循环在int 和unsigned int 之间测试相等性,d被升级为unsigned int 型,-1转换成unsigned int型是一个非常巨大的正整数,使条件表达式始终为假。


I thought the following program was a perfect C program. But on compiling, I found a silly mistake. Can you find it out (without compiling the program :-) ?

#include<stdio.h>

 

void OS_Solaris_print()

{

        printf("Solaris - Sun Microsystems/n");

}

 

void OS_Windows_print()

{

        printf("Windows - Microsoft/n");

 

}

void OS_HP-UX_print()

{

        printf("HP-UX - Hewlett Packard/n");

}

 

int main()

{

        int num;

        printf("Enter the number (1-3):/n");

        scanf("%d",&num);

        switch(num)

        {

                case 1:

                        OS_Solaris_print();

                        break;

                case 2:

                        OS_Windows_print();

                        break;

                case 3:

                        OS_HP-UX_print();

                        break;

                default:

                        printf("Hmm! only 1-3 :-)/n");

                        break;

        }

 

        return 0;

}

hint

solution: 这个不仔细看真看不出来,变量的命名由字母和数字组成,第一个字符必须是字母,下划线"_"被看做是字母。OS_HP-UX_print不符合命名规范

 

3:

   输出结果为 1,而不是 1一直到14,我第一次就弄错了,

   在while和do-while语句中,continue语句的执行意味着立即执行测试部分;

   在for循环中,则意味着使控制转移到递增循环变量部分。

 

4:

   stdout是带缓冲的,不换行的话会出现问题

 

5:

   参见K&R the c lang programming P229,两个特殊的运算符 #和##会影响替换过程

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值