Output difference in gcc and turbo C

前端 未结 7 517
野趣味
野趣味 2020-12-15 22:46

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

#include 

        
7条回答
  •  Happy的楠姐
    2020-12-15 23:41

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

    [C]

    From ISO C99 (Section 6.4.5/6)

    It is unspecified whether these arrays are distinct provided their elements have the appropriate values.

    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.

    Unspecified Behavior: Use of an unspecified value, or other behavior where this International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance


    [C++]

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

    Whether all string literals are distinct(that is, are stored in non overlapping objects) is implementation defined.

    In C++ your code invokes Implementation defined behaviour.

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


    Also see this question.

提交回复
热议问题