Modifying String Literal [duplicate]

Why is there no output when i run this program.

#include<stdio.h>

int main()
{
    char* t="C++";
    t[1]='p';
    t[2]='p';
    printf("%s",t);
    return 0;
}

A C string literal creates an anonymous array of char. Any attempt to modify that array has undefined behavior. Ideally this would be enforced by making the array const, but C didn't always have const, and adding it to string literals would have broken existing code.

char* t="C++";

This is legal but potentially risky. The array containing the characters 'C', '+', '+', '\0' could be stored either in read-write memory or in read-only memory, at the whim of the compiler.

t[1]='p';

Here your program's behavior is undefined, because you're attempting to modify the contents of a string literal. The compiler isn't required to warn you about this, either at compile time or at run time -- nor is it required to do anything to make it "work".

If you want to let the compiler know that the string is read-only, it's best to add the const qualifier yourself:

const char *t = "C++";

The compiler should then at least warn you if you attempt to modify the string literal -- at least if you attempt to do so through t.

If you want to be able to modify it, you should make t a writable array:

char t[] = "C++";

Rather than making t a pointer that points to the beginning of "C++", this makes t an array into which the contents of "C++" are copied. You can do what you like with the contents of t, as long as you don't go outside its bounds.

Some more comments on your code:

#include<conio.h>

<conio.h> is specific to Windows (and MS-DOS). If you don't need your program to work on any other systems, that's fine. If you want it to be portable, remove it.

void main()

This is wrong; the correct declaration is int main(void) (int main() is questionable in C, but it's correct in C++.)

printf("%s",t);

Your output should end with a newline; various bad things can happen if it doesn't. Make this:

printf("%s\n", t);

(The question originally included this line just before the closing } :

getch();

The OP later removed it. This is Windows-specific. It's probably necessary to keep your output window from closing when the program finishes, an unfortunate problem with Windows development systems. If you want a more standard way to do this, getchar() simply reads a character from standard input, and lets you hit Enter to finish (though it doesn't give you a prompt). Or, if you're running the program either from an IDE or from a command prompt, most of them won't close the window immediately.)

Finally, since main returns a result of type int, it should actually do so; you can add

return 0;

before the closing }. This isn't really required, but it's not a bad idea. (C99 adds an implicit return 0;, but Microsoft doesn't support C99.) (Update in 2019: Microsoft's support for C99 features is slightly better. I'm not sure whether the return 0; is necessary.)


"C++" is a string literal stored in read only location and hence cannot be modified. With this -

char* t="C++"; // t is pointing to a string literal stored in read only location

Instead, you should have -

char t[] = "C++" ;  // Copying the string literal to array t

to actually do -

t[1] = 'p' ;