在海湾合作委员会和Turbo C的输出差异 [英] Output difference in gcc and turbo C

查看:124
本文介绍了在海湾合作委员会和Turbo C的输出差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会出现的时候,code使用两种编译器编译产生的输出差异 GCC 的Turbo C

Why is there a difference in the output produced when the code is compiled using the two compilers gcc and turbo c.

#include <stdio.h>

int main()
{    
    char *p = "I am a string";
    char *q = "I am a string";

    if(p==q)
    {
        printf("Optimized");
    }
    else{
        printf("Change your compiler");
    }
    return 0;
}

我得到优化 GCC 改变你的编译器的Turbo C 。为什么呢?

推荐答案

您的问题已标记的c以及C ++。所以我想回答两个的语言。

Your questions has been tagged C as well as C++. So I'd answer for both the languages.

[C]

从ISO C99(第6.4.5节/ 6

这是unspeci网络编辑这些阵列是否提供了不同的元素具有适当的值。

这意味着它是未指定是否 P 都指向同一个字符串或没有。在 GCC 它们都指向我是一个字符串的情况下(GCC优化您的code)而在的Turbo C 他们没有。

That means it is unspecified whether p and q are pointing to the same string literal or not. In case of gcc they both are pointing to "I am a string" (gcc optimizes your code) whereas in turbo c they are not.

<子> Unspeci网络版的行为:
一个unspeci科幻ED值,或其他行为,其中本标准规定的用途
两个或两个以上的可能性,并规定了这是选择在没有任何进一步的要求
例如

[C ++]

从ISO C ++ - 98(第2.13.4 / 2

From ISO C++-98 (Section 2.13.4/2)

是否所有的字符串是不同的(也就是说,存储在非重叠的对象)是实现定义。

在C ++中的code调用实现定义的行为。

In C++ your code invokes Implementation defined behaviour.

<子>实施德科幻奈德行为:
Unspeci网络版的行为,每个实施文件的选择是如何制造

Implementation-defined Behavior: Unspecified Behavior where each implementation documents how the choice is made

另请参阅 问题

Also see this question.

这篇关于在海湾合作委员会和Turbo C的输出差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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