-2
#include<stdio.h>

int main ()
{
    char *s="FIGHT" ;
    printf("\n Whole string is %s ", s );            // Printing FIGHT -- this is fine
    s[0]='L' ;
    printf ("\n Now whole string is %s", s );  // Printing LIGHT -- My Question is how string literal constant is getting modified when it is being stored in read only memory .
}

Above Code is working fine on my system.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
theartist33
  • 470
  • 1
  • 6
  • 13
  • 1
    Why is it printing "AKHIL", again? – Eugene Sh. Jul 27 '15 at 17:21
  • 1
    What? 'I did an obviously bad thing, is it OK because it seems to work on my box?' – Martin James Jul 27 '15 at 17:21
  • Sorry it's not printing AKHIL , it's printing FIGHT ...( typo my bad ) – theartist33 Jul 27 '15 at 17:22
  • I'm fighting to keep up with the edits.... – Martin James Jul 27 '15 at 17:23
  • 3
    You should be thankful it did not print out your credit card number along with your PIN. :P – Sourav Ghosh Jul 27 '15 at 17:23
  • Oh.. I got 5185253654265376:4160' on my system. – Martin James Jul 27 '15 at 17:24
  • ideone is giving a runtime error. Do you really want to use such a code, giving different results for everyone? – Eugene Sh. Jul 27 '15 at 17:26
  • I understand what you mean , point is if concept of storing literals is in read only memory then this should have been same irrespective of compiler you use , because this is something related to memory allocation and their access/mode of access , how come on my system compiler is allowing to modify string literal ( even if I accept the fact of undesired behavior but why even it is being allowed to write/modify read-only-memory (constant area if so) . – theartist33 Jul 27 '15 at 17:34
  • "Why" is the another question. And, I believe, related more to the OS rather than the compiler. – Eugene Sh. Jul 27 '15 at 17:38
  • It may work on some systems or with an old gcc which has the `-fwritable-strings` option. But it's a bad idea writing code using this. Don't do it! Use named character arrays instead. – 4566976 Jul 27 '15 at 18:07
  • okay , I agree that string literals should not be tried to modified , one thing I would like to know that like string literals, does const keyword associated with any data type stores that vairable (with const data type ) in read only memory or it's just get stored in stack memory but with some flag (that this variable is const and supposedly should not be attempted to change ) – theartist33 Jul 28 '15 at 06:25

1 Answers1

6

TL;DR -- Never.

Any attempt to modify a string literal invokes undefined behavior.

To quote the C11 standard, chapter §6.4.5, String literals

[...]. If the program attempts to modify such an array, the behavior is undefined.

§ "Above Code is working fine on my system".

Yes, welcome to the world of undefined behavior, which includes working as (wrongly) expected.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • but code was working fine .. no undefined behavior , also no compile time warning / error – theartist33 Jul 27 '15 at 17:23
  • 1
    @theartist33 See [this](https://stackoverflow.com/questions/1455970/cannot-modify-c-string?rq=1) – edmz Jul 27 '15 at 17:25
  • 5
    ^^ umm... you seem to have misunderstood the last sentence of this answer? – Martin James Jul 27 '15 at 17:25
  • 2
    @theartist33: Undefined behaviour means everything can happen, including what you'd like the program to do. But it's just luck (or rather bad luck, because sooner or later it will fail anyway, and then you will be up for a long bug-hunting session). – Christian Hackl Jul 27 '15 at 17:37
  • I understood it now, thanks,however not sure why my compiler compiling program without any warning – theartist33 Jul 27 '15 at 17:55
  • @theartist33: "Undefined behavior" simply means that the compiler is not required to handle the situation in any particular way. The compiler *may* issue a diagnostic, but it doesn't have to. The compiler *may* generate code that does exactly what you expect, but it may also generate code that does something completely different. Some platforms place string literals in read-only memory, so attempting to update the literal will cause a run-time error. Some platforms don't. Safest course of action is to never attempt to modify a string literal. – John Bode Jul 27 '15 at 18:53
  • okay , I agree that string literals should not be tried to modified , one thing I would like to know that like string literals, does const keyword associated with any data type stores that vairable (with const data type ) in read only memory or it's just get stored in stack memory but with some flag (that this variable is const and supposedly should not be attempted to change ) – theartist33 Jul 28 '15 at 09:18