有些人能解释这种现象?仅2 code线 [英] Some one can explain this behavior ? just 2 lines of code

查看:134
本文介绍了有些人能解释这种现象?仅2 code线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解释一下这个片断:

 的#include<&stdio.h中GT;诠释看跌期权(为const char *海峡){
    的fputs(世界,你好\\ N!,标准输出);
}诠释主(){
    的printf(再见\\ n);
}

输出:<!code>世界,你好返回1​​3


解决方案

这是编译器特定。你与 GCC 此行为。这里还有一些细节。


  • 既然你的#include&LT; stdio.h中&GT; (实际上,因为你是在托管环境)的看跌是C99的标准,并重新定义它是

    未定义行为>

  • GCC编译器有一些优化一些的printf 来变换的的更快看跌期权的顺序。这是合法的,因为你已经包括&LT; stdio.h中&GT; (和C99标准定义了的printf 应该做的在这种情况下,GCC会突破 __ builtin_printf 作为一个中间步骤)


如果您编译 -ffreestanding 你不会观察到。

您的问题非常接近这个;所以这个答案也有关。

Kindly explain this snippet:

#include <stdio.h>

int puts(const char *str) {
    fputs("Hello world!\n", stdout);
}

int main() {
    printf("Goodbye\n");
}

Output : Hello world! return 13

解决方案

It is compiler specific. You get this behavior with GCC. Here are some details.

  • since you #include <stdio.h> (actually because you are in a hosted environment) the puts is that of the C99 standard, and redefining it is undefined behavior

  • the GCC compiler has some optimizations to transform some printf to a sequence of faster puts. This is legal, since you have included <stdio.h> (and the C99 standard defines what printf should do in that case; GCC goes thru __builtin_printf as an intermediate step)

If you compile with -ffreestanding you won't observe that.

Your question is very close to this one; so this answer is also relevant.

这篇关于有些人能解释这种现象?仅2 code线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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