第三十四题

The following is a piece of C code, whose intention was to print a minus sign  times. But you can notice that, it doesn't work.
#include <stdio.h>
int main()
{
int i;
int n = ;
for( i = ; i < n; i-- )
printf("-");
return ;
}
Well fixing the above code is straight-forward. To make the problem interesting, you have to fix the above code, by changing exactly one character. There are three known solutions. See if you can get all those three.

题目讲解:

for( i = ; i < n; i-- )

改成

for( i = ; i < n; n-- )

第三十五题

What's the mistake in the following code?
#include <stdio.h>
int main()
{
int* ptr1,ptr2;
ptr1 = malloc(sizeof(int));
ptr2 = ptr1;
*ptr2 = ;
return ;
}

题目讲解:

int* ptr1,ptr2;

ptr1是指针,ptr2不是指针

改成

int *ptr1,*ptr2;

第三十六题

What is the output of the following program?
#include <stdio.h>
int main()
{
int cnt = , a; do {
a /= cnt;
} while (cnt --); printf ("%d\n", a);
return ;
}

题目讲解:

cnt减到最后为0,运行后有“trap divide error”“floating point exception”的错误。

第三十七题

What is the output of the following program?
#include <stdio.h>
int main()
{
int i = ;
if( ((++i < ) && ( i++/)) || (++i <= ))
;
printf("%d\n",i);
return ;
}

题目解答:

i的值为8。先执行(++i < 7),此表达式的值为0,i=7,由于逻辑运算符的短路处理,(i++/6)跳过执行,
((++i < 7) && ( i++/6))值为0,接着执行(++i <= 9),i的值最终为8。

C puzzles详解【34-37题】的更多相关文章

  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详解【31-33题】

    第三十一题 The following is a simple C program to read and print an integer. But it is not working proper ...

  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. jsp+javaBean 计算器实例

    package com.wzh.test.domain; import java.math.BigDecimal; public class CalculatorBean { private Stri ...

  2. NSSet类型 以及与NSArray区别

    NSSet到底什么类型,其实它和NSArray功能性质一样,用于存储对象,属于集合: NSSet  , NSMutableSet类声明编程接口对象,无序的集合,在内存中存储方式是不连续的,不像NSAr ...

  3. ios8消息快捷处理——暂无输入框

    if (isiOS8) { //ios8的远程推送注册 NSSet *set = nil; #if 1 //1.创建消息上面要添加的动作(按钮的形式显示出来) UIMutableUserNotific ...

  4. English article1

    1. Midlife for many is a time of transition, a time of questioning and a time of change, not just ph ...

  5. HtmlparseUtil.java

    该类并不是一个通用的工具类,需要按自己的要求实现,这里只记录了Htmlparse.jar包的一些用法.仅此而已! 详细看这里:http://gundumw100.iteye.com/blog/7043 ...

  6. Enumerator yielder.yield 与 Proc.yield 区别

    最近看ruby cookbook遇到这个用法,google一下,这里原文解释 http://stackoverflow.com/questions/18865860/enumerator-yielde ...

  7. 20145305 《Java程序设计》第8周学习总结

    教材学习内容总结 1.NIO使用频道来衔接数据节点,可以设定缓冲区容量,在缓冲区中对感兴趣的数据区块进行标记,提供clear().rewind().flip().compact()等高级操作 2.想要 ...

  8. [SQL]SQL删除数据的各种方式总结

    SQL删除数据的各种方式总结 一.使用DELETE从表中删除目标行.记录每次删除操作.如: USE pubs DELETE FROM authors WHERE au_lname = 'McBadde ...

  9. nyoj 102 次方求摸 快速幂

    点击打开链接 次方求模 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 求a的b次方对c取余的值 输入 第一行输入一个整数n表示测试数据的组数(n<100) 每组测 ...

  10. Java学习笔记——static关键字与静态的使用方法

    static:可以修饰成员变量和成员方法. 当变量被static修饰后,则其可以直接被类名调用.类名.成员. static特点: 随着类的加载而加载: 优先于对象存在: 被所有的对象共享,节省空间,但 ...