C puzzles详解【34-37题】
第三十四题
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题】的更多相关文章
- C puzzles详解【51-57题】
第五十一题 Write a C function which does the addition of two integers without using the '+' operator. You ...
- C puzzles详解【46-50题】
第四十六题 What does the following macro do? #define ROUNDUP(x,n) ((x+n-1)&(~(n-1))) 题目讲解: 参考:http:// ...
- C puzzles详解【38-45题】
第三十八题 What is the bug in the following program? #include <stdlib.h> #include <stdio.h> # ...
- C puzzles详解【31-33题】
第三十一题 The following is a simple C program to read and print an integer. But it is not working proper ...
- C puzzles详解【26-30题】
第二十六题(不会) The following is a simple program which implements a minimal version of banner command ava ...
- C puzzles详解【21-25题】
第二十一题 What is the potential problem with the following C program? #include <stdio.h> int main( ...
- C puzzles详解【16-20题】
第十六题 The following is a small C program split across files. What do you expect the output to be, whe ...
- C puzzles详解【13-15题】
第十三题 int CountBits(unsigned int x) { ; while(x) { count++; x = x&(x-); } return count; } 知识点讲解 位 ...
- 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 ...
随机推荐
- Python中if __name__ == "__main__": 的作用
在很多python脚本中在最后的部分会执行一个判断语句if __name__ == "__main__:",之后还可能会有一些执行语句.那添加这个判断的目的何在? 在python编 ...
- 跨应用Session共享
摘要:虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术.本文将详细讨论session的工作机制并且对在Java ...
- SVN版本管理trunk及branch相关merge操作
先说说什么是branch.按照Subversion的说法,一个branch是某个development line(通常是主线也即trunk)的一个拷贝,见下图: branch存在的意义在于,在不干扰t ...
- 20145305《JAVA程序设计》实验二
实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5.了解设计模式 实验要求 1.没有Linux基础的同学 ...
- linux命令行与shell脚本编程大全---bash shell命令
PS1 =“[\t][\u]\$” //新的shell提示符显示了当前时间和用户名 在Windows中,你经常看到这样的文件路径:D:\work\FTL moniqi ...
- lucene-源码分析
lucene-源码分析 http://www.cnblogs.com/forfuture1978/p/3940965.html
- .NET基础操作回顾_使用ADO.NET操作SqlServer使用的类
有些工具用的久了或者有新工具出现后,就慢慢的遗忘了很多,它们从熟悉的变成陌生,当然,对于我们来说不是好事吧. 今天回顾一下ADO.NET用到的MS的基础类库,先上代码(标准的SqlServer操作) ...
- Rman-03002,Rman-12010,Rman-12012
为什么会出现如此的错误呢? 答:因为我们在分配通道时设备类型分配为磁带,但是你并没有安装磁带设备,所以就会出现这样的错误. 错误如下: 解决方法: 登陆你的目标数据库,重新进行设置,命令如下: con ...
- Python实现顺时钟回形矩阵
无意间在网上看到了一个面试题是,写出一个回形矩阵.实现的效果大致如下: [ 1, 2, 3, 4, 5] [16, 17, 18, 19, 6] [15, 24, 25, 20, 7] [ ...
- 【Robot Framework】robot framework 学习以及selenium、appnium、requests实践(三)
看了上一章的内容,想必较为简单的case也都会编写了吧,但是是不是觉得,如果能够实现用例参数化,是不是会节省很多劳动力,这节就来学下RF中的user keywords,会让你发现写用例原来可以这么简单 ...