第三十四题

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. CentOS7 安装MongoDB 3.0服务

    1,下载&安装 MongoDB 3.0 正式版本发布!这标志着 MongoDB 数据库进入了一个全新的发展阶段,提供强大.灵活而且易于管理的数据库管理系统.MongoDB宣称,3.0新版本不只 ...

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

    教材学习内容总结 1.只有Lambda表达式,参数的类型必须写出来,如果有目标类型,在编译程序可推断出类型的情况下,可以不写出 2.Lambda表达式本身是中性的,不代表任何类型的实例,可用来表示不同 ...

  3. grunt构建前端自动化的开发环境

    废话不多说.直奔主题. 1.安装node. 别问为什么.如果你不知道,说了你还是不知道. 别问怎么安装,自己去百度. 2.安装grunt_CLI. 安装完node,并且安装成功了,后.下载grunt_ ...

  4. InnoSetup打包exe安装应用程序,并添加卸载图标 转

    http://blog.csdn.net/guoquanyou/article/details/7445773 InnoSetup真是一个非常棒的工具.给我的印象就是非常的精干.所以,该工具已经一步步 ...

  5. PostgreSQL 数据迁移

    新主机PostgreSQL需要事先建立和原主机名称相同的用户和数据库. 备份原主机数据库 pg_dump -U <UserName> -p <PortNum> <DBNa ...

  6. c++cin.ignore()

    c++ 中cin.ignore(100,'\n'); 的作用是清除输入流中多余的字符请问这句话是什么意思? 可以举个例子吗? 提问者采纳 这个其实就是忽略cin中的前100个字符,或是'\n'之前的字 ...

  7. List<IPoint> to IPointCollection to IPolygon

    IPointCollection 到 IPolygon的转换 IPoint pPoint = new PointClass();            //IPolygon pPolygon1 = n ...

  8. mysql 基本使用教程(源于网络)

    http://dev.mysql.com/doc/refman/5.1/zh/index.html 3.1. 连接与断开服务器 3.2. 输入查询 3.3. 创建并使用数据库 3.3.1. 创建并选择 ...

  9. html+css源码之实现登录弹出框遮罩层效果

    在web开发中,很多网站都做了一些特别炫丽的效果,比如用户登录弹框遮罩层效果,本文章向大家介绍css如何实现登录弹出框遮罩层效果,需要的朋友可以参考一下本文章的源代码. html+css实现登录弹出框 ...

  10. html 去掉input 获取焦点时的边框

    html中,当input标签获取焦点的时候(例如,当光标放在input框中准备输入值时), input标签外围会出现边框,有的时候我们需要去掉这个边框,可以使用css的outline:none;属性将 ...