问题描述
void main
{
int x;
if(x)
printf(" Hello" ;);
else
printf(" World");
}
可以给出什么值到x使得输出为Hello
World?
void main
{
int x;
if(x)
printf("Hello");
else
printf("World");
}
What value can be given to x such that the output is "Hello
World"?
推荐答案
我认为你歪曲了这个技巧问题。
使用`x`和`int`它将无法工作。
但是,如果你适当地#define x`,你可以获得所需的
效果。例如:
#define x(!printf(" Hello"))
就可以了。显然,一旦你将你的代码更改为实际上类似于C的
:
#include< stdio.h>
#define x(!printf(" Hello,"))
int main(无效)
{
if(x)
printf(" Hello");
else
printf(" World");
printf(" \ n");
返回0;
}
I think you misrepresented the trick question.
With `x` an `int` it will never work.
However, if you `#define x` appropriatelly, you can get the desired
effect. E.g.:
#define x (!printf("Hello "))
will do the trick. Obviously, once you correct your code to something
that actually resembles C:
#include <stdio.h>
#define x (!printf("Hello, "))
int main(void)
{
if(x)
printf("Hello");
else
printf("World");
printf("\n");
return 0;
}
没有一个。
如果你没有意思/价值/,但/ /初始化/,那么很容易。
-
Chris"但if if变得无关紧要 Dollin
人们是设计的一部分。忘记这一点是危险的。 / Star Cops /
There isn''t one.
If you didn''t mean /value/, but /initialiser/, then easy-peasy.
--
Chris "but the if becomes irrelevant" Dollin
"People are part of the design. It''s dangerous to forget that." /Star Cops/
程序的输出未定义不少于三个不同
原因。所以它可能会说Hello World。如写的。或者,当然,它可能不是
。
除非您了解这三个原因,我建议您花费
$ b $减少拼图问题的时间,花更多时间阅读好书。
-
Richard Heathfield
" Usenet是一个奇怪的地方 - dmr 29/7/1999
电子邮件:rjh在上面的域名(但显然放弃了www)
The output of the program is undefined for no fewer than three different
reasons. So it might well say "Hello World" as written. Or, of course, it
might not.
Until you understand what the three reasons are, I recommend that you spend
less time with puzzle questions and more time with a good C book.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
这篇关于if-else的一招的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!