[LCP]query
Greg Black
gjb at gbch.net
Fri Feb 1 21:47:44 UTC 2002
Santosh.Kawade at eyield.com wrote:
| I have a query in C which has baffled me for sometime...
| Please consider the following C programme -
|
| 1 #include <stdio.h>
| 2
| 3 void abc();
| 4 main()
| 5 {
| 6 abc();
| 7 abc();
| 8 }
| 9
| 10 void abc()
| 11 {
| 12 char *p = "raman";
| 13 printf("%s\n",p);
| 14 strcpy(p, "shyam");
| 15 }
|
| The O/P of the program is -
| raman
| shyam
An equally likely outcome is:
raman
Bus error (core dumped)
| Though one would expect that the word 'raman' will be printed twice.
One would have no expectations at all -- the program is
incorrect and its behaviour is explicitly undefined by the
standard.
You cannot write to a string literal.
| I think that the O/P obtained is related to the fact that string
| literals are stored in data segment.
The reasons for the output are of little interest. Your code is
wrong and it can do anything at all.
Greg
More information about the linuxCprogramming
mailing list