if-else的一招 [英] A trick with if-else

查看:74
本文介绍了if-else的一招的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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"?

推荐答案



Piyush Agarwal写道:

Piyush Agarwal wrote:
void main
{
int x;
if(x)
printf(" Hello");

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;
}


Piyush Agarwal写道:
Piyush Agarwal wrote:
void main
{


`main `是一个函数返回`int`,而不是一个非函数的

类型`void`和值`麻烦,有一个语法错误`。

int x ;
if(x)
printf(" Hello");

printf(" World");
}
什么价值可以被赋予x使得输出是Hello
World?
void main
{
`main` is a function that returns `int`, not a non-function
of type `void` and value `bother, there was a syntax error`.
int x;
if(x)
printf("Hello");
else
printf("World");
}
What value can be given to x such that the output is "Hello
World"?




没有一个。


如果你没有意思/价值/,但/ /初始化/,那么很容易。


-

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/


Piyush Agarwal说:
Piyush Agarwal said:
void main
{
int x;
if(x)
printf(" Hello");

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"?




程序的输出未定义不少于三个不同

原因。所以它可能会说Hello World。如写的。或者,当然,它可能不是



除非您了解这三个原因,我建议您花费
$ b $减少拼图问题的时间,花更多时间阅读好书。


-

Richard Heathfield

" Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件: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的一招的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆