C 难题:printf 的输出应该是“5”总是
我在一篇 C 能力倾向试卷中发现了这个难题。
void change()
{
//write something in this function so that output of printf in main function
//should always give 5.you can't change the main function
}
int main()
{
int i = 5;
change();
i = 10;
printf("%d", i);
return 0;
}
有什么解决办法吗?
I found this puzzle in a C aptitude paper.
void change()
{
//write something in this function so that output of printf in main function
//should always give 5.you can't change the main function
}
int main()
{
int i = 5;
change();
i = 10;
printf("%d", i);
return 0;
}
Any solutions.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
定义?
define?
这是一个 POSIX 答案,确实满足了问题的要求:)
它在某些架构/编译器上不起作用,但在这里可以。
编辑:这对于抵制标头的人来说应该更加强大。
This is a POSIX answer that really does what the problem asks :)
It won't work on some architectures/compilers but it does here.
EDIT: This should make it more robust for people with boycotting headers.
这是一个非常便宜的答案:
:-P
Here's a really cheap answer:
:-P
这是另一种可能性:
这比更改返回地址更可移植,但要求您(a)有一个可以池化字符串文字的编译器(大多数这样做),并且(b)有一个不会将常量放入读取中的编译器-only 部分,或者在没有 MMU 的架构上运行(现在不太可能)。
Here's another possibility:
This is much more portable than changing the return address, but requires you to (a) have a compiler that pools string literals (most do), and (b) have a compiler that doesn't place constants in a read-only section, or be running on an architecture with no MMU (unlikely these days).
有人想过使用 atexit 吗?
请注意,主函数中没有终止换行符,如果我们向 stdout 发送 2 个退格字符,则 10 将被删除,并且仅打印 5。
Anybody thought of using atexit?
Note that there is no terminating newline in the main function, if we send 2 backspace characters to stdout, the 10 will be erased, and only the 5 will be printed.
调用必要的 #include,并将注释替换为括号不平衡的文本:
测试成功。感谢 dreamlax 和 Chris Lutz 修复了错误。
Invoke the requisite #include, and replace the comment with the parenthesis-unbalanced text:
Tested successfully. Thanks to dreamlax and Chris Lutz for bugfixes.
堆栈中有一个局部变量 i,其初始值为 5。
使用change(),您需要将下一条指令修改为5,因此您需要将缓冲区覆盖到设置为10的位置,并将其设置为5。
You have a local variable i in the stack that has a value of 5 to begin with.
With change(), you need to modify the next instruction to be 5 so you would need to buffer override to that location where 10 is set, and have it set to 5.
main()
中的printf("%d", i);
调用不会以换行符结束其输出,程序的行为是实现定义的。我断言,在我的实现中,无法为最后一行编写终止换行符的程序始终会打印
5
,后跟换行符作为最后一行。因此,无论
change()
的定义如何,输出始终为 5。 : -)(换句话说,这些问题有什么意义,除非它们是在特定的硬件、编译器等上运行的?)
The
printf("%d", i);
call inmain()
doesn't end its output in a newline, the behavior of the program is implementation-defined.I assert that on my implementation, a program that fails to write a terminating newline for the final line always prints
5
followed by a newline as its last line.Thus, the output will always be 5, whatever the definition of
change()
. :-)(In other words, what's the point of such questions, unless they're meant to run on particular hardware, compiler, etc.?)
简单:
稍微复杂一些:
Simple:
Slightly more sophisticated:
我怀疑对此的“正确”答案是修改
change()
函数中堆栈上的返回地址,以便当它返回时控制流会跳过i=10< /code> 命令并直接进入
printf
。如果是这样,那么这是一个可怕的、丑陋的问题,(不可移植的)答案需要了解所使用的体系结构和指令集。
I suspect that the "correct" answer to this is to modify the return address on the stack within the
change()
function, so that when it returns the control flow skips thei=10
command and goes straight to theprintf
.If so then that is a horrible, ugly question and the (non-portable) answer requires knowledge of the architecture and instruction set used.
像这样的怎么样:(仅限x86)
How about something like this: (x86 only)
喜欢这里的答案。我让它分两行工作。
10永远不会到达输出,因为在change()函数打印5之后,stdout文件描述符被关闭。
人们可以使用以下在线 C 编译器来验证这一点。
http://www.tutorialspoint.com/compile_c_online.php
Loving the answers in here. I got it to work in two lines.
The 10 will never reach the output because after the change() function prints a 5, the stdout file descriptor is closed.
People can verify that using the following online C compiler.
http://www.tutorialspoint.com/compile_c_online.php
这是一个不同的:
我可以获得“解决愚蠢问题的最小#define 奖”吗?
here is a different one:
do I get the "smallest #define to solve a silly problem award"?
我不确定这是否总是有效,但是如何在堆栈上定位 i 变量,如下所示:
I am not sure this would always work, but what about locating the i variable on the stack like this: