tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [Tinycc-devel] compare quoted strings


From: Christian Jullien
Subject: RE: [Tinycc-devel] compare quoted strings
Date: Mon, 8 Nov 2010 13:44:38 +0100

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.

Tcc, which is also allowed, makes two copies and addresses are different.
Expression returns 0.

C.

-----Original Message-----
From: address@hidden
[mailto:address@hidden On Behalf Of Henry
Kroll
Sent: lundi 8 novembre 2010 12:36
To: address@hidden
Subject: [Tinycc-devel] compare quoted strings


This little snippet prints "true" if compiled with gcc and "not true" if
compiled with tcc.

int main (void){
    printf ("%sok\n","a"=="a"?"":"not ");
    return 0;
}

Note that "a"=="a" is not what it looks like. We are comparing the address
of the pointer "a" with itself. If the book I just looked at (Banahan, et
al., 1991) interprets the standard correctly, a quoted string serves as a
pointer to the first element, thus "a"=="a" should return true.

Ref

Banahan, Mike, Brady, Declan and Doran, Mark. (1991). The C Book - Character
handling. Chapter 5.4. Retrieved November 8, 2010, from
http://publications.gbdirect.co.uk/c_book/chapter5/character_handling.html



_______________________________________________
Tinycc-devel mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/tinycc-devel
----------------------------------------------------------------------------
-----------
Orange vous informe que cet  e-mail a ete controle par l'anti-virus mail. 
Aucun virus connu a ce jour par nos services n'a ete detecte.









reply via email to

[Prev in Thread] Current Thread [Next in Thread]