What should be the result?

I expect h here. But he shows g. Why?

char *c="geek"; printf("%c",++*c); 
+4
source share
3 answers

You are trying to change a string literal, which is undefined behavior . This means that nothing definite can be said about whether your program will be printed, or whether it really prints anything.

Try:

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

For further discussion, see the FAQ .

+9
source

This is an undefined behaviour as you are trying to modify string literal

* c will give the character 'g' , but when applying this, ++ * c means what you are trying to do

*c=*c+1; which modifies the string and its undefined in the standard language

It is better to use a char array to solve this problem, since the literal "string" is stored in readonly memory, and changing it causes this.

0
source

The expression will be evaluated as follows: (++ (* c)),

First, the inner * C will be evaluated so that it prints 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'.

-1
source

Source: https://habr.com/ru/post/1444754/


All Articles