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 ...
随机推荐
- NSPredicate,谓词
原文地址:http://blog.csdn.net/holydancer/article/details/7380799 在语言上,谓语,谓词是用来判断的,比如“我是程序猿”中的是,就是表判断的谓语, ...
- arm-linux-objdump
一.arm-linux-objdump常用来显示二进制文件信息,常用来查看反汇编代码二.常用选项:1.-b bfdname 指定目标码格式2.—disassemble或者-d 反汇编可执行段3.—di ...
- oracle查询锁和杀锁
查询锁: SELECT l.session_id sid, s.serial#, l.locked_mode,l.oracle_username, l.os_user_name,s.machine, ...
- jquery中各个事件执行顺序如下:
jquery中各个事件执行顺序如下: 1.ajaxStart(全局事件) 2.beforeSend 3.ajaxSend(全局事件) 4.success 5.ajaxSuccess(全局事件) 6.e ...
- 串口总是报'Error opening serial port'
Comm1.CommName := '//./' + Trim(combx_Port.Text); 目前串口大于20 用上面方法解决的 网上也有上面方法解决如下错误的. 若是您已会应用SPCOMM且 ...
- 编译 proto 文件到指定语言的代码
由于 Protocol Buffers 3 的正式版还没有发布,在官网(https://developers.google.com/protocol-buffers/docs/downloads)目前 ...
- [kuangbin带你飞]专题十一 网络流
ID Origin Title 34 / 81 Problem A POJ 3436 ACM Computer Factory 92 / 195 Problem B POJ 3 ...
- oracle行列转换函数的使用
oracle 10g wmsys.wm_concat行列转换函数的使用: 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行 ...
- OC基础(5)
#pragma mark指令 description方法 OC多文件开发介绍 *:first-child { margin-top: 0 !important; } body > *:last- ...
- "HTTP 错误 500.19 请求的页面的相关配置数据无效" 解决办法
HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该页的相关配置数据无效. 问题"详细错误信息模块 IIS Web Core通知 Begin ...