On 11/8/2010 8:44 PM, Christian Jullien wrote:
In fact both gcc and tcc are correct because your snippet uses an undefined
behavior.

"a"=="a" compares the address of two strings which are the address of the
first character, not the character itself as you said.

Now, which one is correct? None or both.

A litteral string is supposed to be constant as if declared:

const char *s="a";

Because litteral strings are constant, a compiler is allowed to share
different strings and define only one instance.
Gcc shares common strings and this "a"=="a" compares the addresses of the
same object. It returns 1.

The OP can get the widely-available C standard draft #1256 and look at:

Annex J
J.1 Unspecified Behavior:
  ...
  - Whether two string literals result in distinct arrays (6.4.5).
  ...

Section 6.4.5 states the same thing. The section on initialization for literal strings also warns about a related kind of unspecified behaviour.

--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia

_______________________________________________
Tinycc-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to