C语言 执行 'If' 和 'else' 块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3988275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing both 'If' as well as 'else' block
提问by Prashant
Possible Duplicates:
Stupid Question Regarding If-Else's Simultaneous Execution in C++ or C
Is it possble to execute both if and else part of an if — else control statement ?
可能的重复:
关于 If-Else 在 C++ 或 C 中同时执行的愚蠢问题
是否可以同时执行 if 和 else 控制语句的一部分?
Hello everyone.. I had a question in an interview like this which i couldn't answer. Consider Following code block. Assume necessary header files.
大家好..我在这样的采访中遇到了一个我无法回答的问题。考虑以下代码块。假设必要的头文件。
if(.......)
{
printf("hello");
}
else
{
printf("world");
}
without moving/adding any code & without use of additional printing statements bring output as "Hello world"..You have to write the missing condition in if statement.. is it possible to execute both blocks by some condition?? Please help
不移动/添加任何代码 & 不使用额外的打印语句将输出作为“Hello world”..你必须在 if 语句中编写缺少的条件..是否可以通过某种条件执行两个块?请帮忙
回答by JeremyP
This will work
这将工作
if(schrodingers_cat_is_dead())
{
printf("hello");
}
else
{
printf("world");
}
Unfortunately, as soon as you look at the output, not only will it collapse into the state of only producing "hello" or "world", but there's a 50% chance you will have murdered a cat.
不幸的是,一旦您查看输出,它不仅会崩溃到只生成“hello”或“world”的状态,而且您有 50% 的机会杀死了一只猫。
回答by Benoit
try fork()! (and join afterwards). That's the only feature involving quantum mechanics that I know of.
试试fork()!(然后加入)。这是我所知道的唯一涉及量子力学的功能。
fork()returns two distinct values at a time, which only the Schr?dinger's cat is capable of.
fork()一次返回两个不同的值,这只有薛定谔的猫才能做到。
NOTE:In a comment, Donal Fellows suggests:
注意:在评论中,Donal Fellows 建议:
You'll get more reliable behavior with
!vfork()
你会得到更可靠的行为
!vfork()
回答by Donal Fellows
If you can put in a macro definition as "part" of that condition...
如果您可以将宏定义作为该条件的“一部分”...
if (1
#define else if (1)
)
{
printf("hello");
}
else
{
printf("world");
}
Then that code will indeed print "helloworld". It's a horrible, dirty trick though; my soul is probably in trouble for mentioning it!
然后该代码确实会打印“ helloworld”。不过,这是一个可怕的、肮脏的把戏;我的灵魂可能因为提到它而陷入困境!
回答by Larry Lustig
It depends on the exact phrasing of the question. As you describe it, the question does notask you to execute both branches of the if statement. It asks you to insert a condition that results in printing "hello world" without changing anything else.
这取决于问题的确切措辞。正如您所描述的,该问题并不要求您执行 if 语句的两个分支。它要求您插入一个条件,导致打印“hello world”而不更改任何其他内容。
If you have complete freedom in what you put in the condition, you can use this solution:
如果您对条件中的内容有完全的自由,则可以使用以下解决方案:
if(printf("hello ") == -1)
{
printf("hello");
}
else
{
printf("world");
}
However, the solution uses "printf" in the condition, which is ruled out by one of the rules you gave. What's not clear to me is whether the printf prohibition also applies to what you write in the condition.
但是,该解决方案在条件中使用“printf”,这被您给出的规则之一排除。我不清楚的是 printf 禁令是否也适用于您在条件中写入的内容。
Note: Answer is edited to reflect the comments. The original answer ignored the prohibition on print statements.
注意:编辑答案以反映评论。原始答案忽略了对打印语句的禁止。
回答by Andrew Sledge
You can, but it's probably bad practice. Seems the interviewer was testing your knowledge of the language. Built with gcc 4.1.2.
你可以,但这可能是不好的做法。看来面试官是在测试你的语言知识。使用 gcc 4.1.2 构建。
#include <stdio.h>
int main() {
if(!printf("hello ")) {
printf("hello");
} else {
printf("world");
}
return 0;
}
回答by Kirit Chandran
There is one possible way, it is not recommended.
有一种可能的方式,不推荐。
if(check==true)
{
printf("dont do this");
goto condition1;
}
else
{
condition1: ////this is crazy
printf("Dont do this ever");
}
回答by Sergio
The easiest way I think is this:
我认为最简单的方法是这样的:
//if(.......)
{
printf("hello");
}
//else
{
printf("world");
}
回答by Adrian Smith
No, it's not possible. Either the first block or the else block will get executed; that is the nature of an ifstatement.
不,这不可能。将执行第一个块或 else 块;这就是if声明的性质。
回答by Hyman
There's no way to execute both branches of an if/else without exploiting it with weird things. If a condition is true then the else won't get executed and viceversa.
如果不使用奇怪的东西来利用它,就无法执行 if/else 的两个分支。如果条件为真,则 else 不会被执行,反之亦然。
They're absolutely mutually exclusive.
它们是绝对互斥的。
回答by andrewmu
Not without iterating over it again. I don't think you can do any Duff device like craziness in if statements. I suppose you could use goto, you say just by writing the condition.
不是没有再次迭代它。我不认为你可以在 if 语句中做任何像疯狂一样的 Duff 设备。我想您可以使用 goto,您只需编写条件即可。

