Comparing literal empty strings in C

In C, what is below?

if ("" == "") { printf("Empty strings are equal\n"); } 

I have a compiler on hand that tells me that "" really equal to "" . But is this equality guaranteed?

Change I understand very well how pointer comparison and string comparison work. C. I ask what behavior, if any, is specified in the C standard for string compilation time constant. I am convinced that the strings are not guaranteed to be equal, but in practice they will usually be equal, since all constant empty strings will be interned to the same address. But I want to know if anyone can give the final link

+4
source share
3 answers

C Standard says (6.4.5 / 6)

It is not indicated whether [string literals] are distinct

+11
source

Guaranteed? I doubt it. You do not compare the contents of the strings, but rather your addresses, which means that you rely on the compiler not to emit two lines of literals that have the same content in one place. It probably works, but not on something you have to rely on (and it’s not clear why this is useful).

Edit: see also Why ""! = "A" in C? - he has an answer basically to the same question with almost a hundred upvotes (and was written by a user whose compiler did it differently).

+5
source

I don’t think there is any guarantee that they will have the same address. I doubt that the standard will require this behavior. Why do you have to depend on how predictable this is?

0
source

All Articles