The code segment given below compiles and when run gives the result as :
$ make gcc -g -Wall -o test test.c $ ./test string /* code1 */
#include<stdio.h> char *somefunc1() { char *temp="string"; return temp; } int main(int argc,char *argv[]) { puts(somefunc1()); return 0; } whereas a slight modification to this code gives different results :
$ make gcc -g -Wall -o test test.c test.c: In function ‘somefunc1’: test.c:5: warning: function returns address of local variable $ ./test /* code 2 */ #include<stdio.h> char *somefunc1() { char temp[] ="string"; return temp; } int main(int argc,char *argv[]) { puts(somefunc1()); return 0; } Why this is happening ?