Alex Rivera | Logout

Output difference in GCC and Turbo C

Asked 2010-07-20T11:18:31.707
14

Why is there a difference in the output produced when the code is compiled using the two compilers GCC and Turbo C.

#include <stdio.h>

int main()
{    
    char *p = "I am a string";
    char *q = "I am a string";

    if(p==q)
    {
        printf("Optimized");
    }
    else{
        printf("Change your compiler");
    }
    return 0;
}

I get Optimized on GCC and Change your compiler on Turbo C. Why?

Edit
Report

1 Answer

14

Since your string literal is a constant expression, i.e. you should not modify it via a pointer, there is no real purpose in storing it in the memory space twice. Being a newer compiler, gcc merges the literals by default while Turbo C does not. It is a sign of gcc's support for the newer language standard that has the notion of const data.

answered 2010-07-20T11:21:50.230

Your Answer