2

I have written the following program:

#include <stdio.h>

main()
{
    if("ddd" == "ddd")
        printf("equal");
    else
        printf("not equal");
}

The output is "equal", but according to me, the output should be "not equal" because the string literals are stored in the literal pool or some read only memory (I guess it depends on OS), so both strings should have two different addresses as they are stored at different addresses in memory.

Previously, I have done the same type of example (one year back), and that time the output was "not equal". Now, could anyone tell me, is this due to a change in the C standard, or am I missing something?

Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • 2
    For comparing string use `strcmp()`. – ani627 Sep 19 '14 at 07:27
  • 7
    There is nothing that prevents the compiler from optimizing by using the same address , as the string literals here are const. – Pradheep Sep 19 '14 at 07:29
  • 1
    Why would somebody ever do something like this? – Mohit Jain Sep 19 '14 at 07:37
  • @AlexisKing This question asks why it's equal, not the other way. So technically, not a duplicate in my opinion, although the same answer can answer both questions. – Yu Hao Sep 19 '14 at 07:42
  • @YuHao The answers on that question do indeed answer this question, mentioning that compilers *may* produce code that results in equality, but it's not required one way or the other by the C standard. – Alexis King Sep 19 '14 at 07:44
  • @AlexisKing Like [this Meta answer](http://meta.stackoverflow.com/a/254590/1009479) says, *many questions have similar or identical answers but are not duplicates.* After all, this question asks why it's equal, and the other question asks why it's not equal. But that's just my opinion. – Yu Hao Sep 19 '14 at 07:54
  • @YuHao I would argue that the two questions are effectively the same thing, but I suppose that's subjective. They're both about string literal reference equality, which is fairly specific as-is. – Alexis King Sep 19 '14 at 07:55
  • @AlexisKing, i as a user first of all, search for exact requirements. i already knew that "a" != "a" so i didn't searched for that thread. for example - u alread know that office is closed on sunday . will u go and check whether it is open or not? – debendra nath tiwary Sep 19 '14 at 08:58
  • ok, thank you all . so i finally concluded that result may vary from compiler to compiler , as compiler may keep only 1 copy in literal pool if strings are same OR it may keep same string at 2 different location. correct me if i m wrong. – debendra nath tiwary Sep 19 '14 at 10:44

3 Answers3

6

It's unspecified for string literals with the same content to have the same address or not. So the output of your program could be equal or it could be not equal, your compiler happens to put them in the same place.

C11 6.4.5 String literals

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

Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
5

Of course, what you do in that condition is a comparison between pointers (use strcmp to compare C strings).

So, I think it is a compiler translation/optimization that "maps" identical literals at the same location in the memory.

EDIT 1:

The following sample confirms what I wrote:

#include <stdio.h>

char* a = "ddd";
char* b = "ddd";
char* c = "ddd";

int main() {
    printf ("a => %p\nb => %p\nc => %p\n", a, b, c);
}

The previous program, compiled with gcc using -O0 and executed will print:

a => 0x40060c
b => 0x40060c
c => 0x40060c

I don't know how other compilers will treat the same situation.

Filippo Lauria
  • 1,965
  • 14
  • 20
  • 1
    , i have already removed optimisation (cc -O0 prog.c , i guess there is still some optimization there) then also compiler is storing same string literals in one memory location only. thank you – debendra nath tiwary Sep 19 '14 at 10:48
1

When you're comparing two character values (which are not pointers), it is a numeric comparison.

But when you're comparing two string, Base address of strings are compared.If supposed compilers treats as both string are in same location ,then o/p is equal.Otherwise Not.

What you are comparing the two memory addresses for the different strings, which are stored in different locations.so,Not equal.

Even it is read only memory,you are used this for comparison only.You are not modify or not write anything.

Anbu.Sankar
  • 1,326
  • 8
  • 15