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 ...
随机推荐
- .NET 请求被挂起,前端轮询,委托
起因:因项目需要监控方法中计算进度,故而想通过AJAX调用,返回前端显示进度,结果开发中遇到第二个AJAX请求被挂起,需要等到第一个请求(计算)完成后,才会被处理到. 百度种种,知其原因是在Sessi ...
- 认识与学习BASH(中)
1.在设置变量中:单引号与双引号的最大不同:双引号能保有变量的内容,单引号仅能是一般字符 2.反单引号(`)作用:在一串指令中,在‘之内的指令将会被先执行,其结果将作为外部的输入信息. locate指 ...
- Django 的 CSRF 保护机制
转自:http://www.cnblogs.com/lins05/archive/2012/12/02/2797996.html 用 django 有多久,我跟 csrf 这个概念打交道就有久了. 每 ...
- [SQL]声明触发器 <待整理>
./*声明触发器 create trigger dl_stu_mess4 on student for delete as declare @name_id int select @name_id=s ...
- 页面设计--Grid列表
Grid列表控件 功能:主要实现Html Table功能,支持多表头.固定表头.固定列.输出.组合查询.编辑功能 优点:可以快速的通过数据集合生成多表头.多样式的列表.通过简单的拖拉实现多层表头. G ...
- elasticsearch从mysql导入数据
详细:https://github.com/jprante/elasticsearch-jdbc(最下面有各数据库的导入方法说明) elasticsearch版本为1.5.2 1.下载 elastic ...
- (转)使用Ping获得局域网机器信息
实际编程中会遇到查找局域网机器的情况,这个时候ping命令对我们帮助比较大,一般来说,都是在命令行中输入“ping XXX -t”,下面的方法是在C#编码中使用Ping. 原文地址:http://zh ...
- Servlet跳转到Jsp的指定div
问题: 首页点击一个连接,切换div(id = cc_bi) <div id="K_a"> <a href ...
- 稀疏矩阵乘法加法等的java实现
原创声明:本文系作者原创,转载请写明出处. 一.前言 前几天由于科研需要,一直在搞矩阵的稀疏表示的乘法,不过最近虽然把程序写出来了,还是无法处理大规模的矩阵(虽然已经是稀疏了).原因可能是 ...
- SMTP邮件发送命令
第一步,远程登录smtp服务器 在命令行窗口输入 telnet smtp.163.com 25 然后回车第二步,用户登录 输入 helo 163.com 回车,这是向服务器表明你的用户身份250 OK ...