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 ...
随机推荐
- centos update git(转载)
From:http://itekblog.com/update-git-centos/ 1.下载RPMForge repo cd /tmp # bit: wget http://pkgs.repofo ...
- Ubuntu查看磁盘空间命令(转载)
linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...
- How to push your code in git
1. display all the branches git branch -a 2. delete branches git br -d <branch> # 删除某个分支 git b ...
- iPhone的定位技术与Core Location框架
来源:http://www.cnblogs.com/lovecode/archive/2011/12/24/2300579.html iPhone定位来源通常有:1. GPS定位 2. WiFi定位 ...
- java记录在线人数小案例
文件目录: web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...
- APKTool 提取APK文件的资源
APK文件本身是一个压缩包,直接用解压工具即可打开,但里面的文件都已被编码为二进制文件格式,不能直接看,比如程序描述文件AndroidManifest.xml. 使用apktool工具可以将这些文件解 ...
- Plan9 与 Plan9port
Plan9 Plan9 是一个操作系统.由贝尔实验室开发的,其主要的负责人是Rob Pike(现在在google工作,负责Go语言的开发). 参考:http://www.cnblogs.com/yjf ...
- [SQL]合并字符串
--带符号合并行列转换 --有表t,其数据如下: /* a b 1 1 1 2 1 3 2 1 2 2 3 1 --如何转换成如下结果: a b 1 1,2,3 2 1,2 3 1 */ drop t ...
- (转)C# Color类图示
本文来源 http://www.cnblogs.com/lv8218218/archive/2010/12/20/1911746.html
- js的传值,table中tr的遍历,js中动态创建数组
1.这里关键是对页面中的传值,其次是动态的创建一个数组,用来存值 $(val).css("background-color", "rgb(251, 248, 233)&q ...