C code:怎么这些甚至工作? [英] C Code: How does these even work?

查看:121
本文介绍了C code:怎么这些甚至工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚看到这个这里

#include <stdio.h>

int main(int argc, char *argv[printf("Hello, world!\n")]) {}

这样做是打印的Hello World!

What this does is print "Hello World!"

但实际上回事?

我想最好的是,它被编译并执行堆栈的顶部抛出,但语法甚至不看法律,我...

The best I can guess is that it gets compiled and thrown at the top of the execution stack, but the syntax doesn't even look legal to me ...

推荐答案

在code利用C99的变长数组的功能,它可以让你声明数组的大小只有在运行时是已知的。 的printf 返回等于实际上打印的字符数的整数,因此code打印你好,世界!第一,使用返回值的argv 的大小。在函数本身什么都不做。 实际调用的printf 本身很可能进入由编译器,生成的启动code依次调用

The code makes use of C99's variable-length array feature, which lets you declare arrays whose size is known only at run-time. printf returns an integer equal to the number of characters that were actually printed, so the code prints "Hello, world!" first and uses the return value as the size of argv. The main function itself does nothing. The actual call to printf itself probably goes into the startup code generated by the compiler, which in turn calls main.

编辑:我只是检查由 GCC产生的code 的拆卸,看来,在调用的printf 走了进去本身,在任何其他code。

I just checked the disassembly of the code generated by gcc and it appears that the call to printf goes inside main itself, before any other code.

这篇关于C code:怎么这些甚至工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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