Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should be the output?

Tags:

c

pointers

I'm expecting h here.But it is showing g. Why?

char *c="geek"; 
printf("%c",++*c);
like image 411
Amit Gupta Avatar asked Feb 18 '23 13:02

Amit Gupta


1 Answers

You are attempting to modify a string literal, which is undefined behaviour. This means that nothing definite can be said about what your program will print out, or indeed whether it will print anything out.

Try:

char c[]="geek"; 
printf("%c",++*c);

For further discussion, see the FAQ.

like image 76
NPE Avatar answered Feb 27 '23 14:02

NPE