Reputation: 287
I'm expecting h here.But it is showing g. Why?
char *c="geek";
printf("%c",++*c);
Upvotes: 4
Views: 101
Reputation: 500673
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.
Upvotes: 9
Reputation: 178
The expression will be evaluated like this (++(*c)),
First the inner *C will be evaluated so it will print g. then the increment operator will be applied to the pointer variable C. After this print statement the pointer c will point to the next character 'e'.
Upvotes: -1
Reputation: 9214
It's undefined behaviour
since you are trying to modify the string literal
*c will give character 'g'
but when you apply this ++*c means you are trying to do
*c=*c+1;
which is modifying the string and its undefined in language standard
It's better to use char
array to solve this since "string" literal are stored in readonly memory and modifying it causes this.
Upvotes: 0