The output of this code is "hhe!". Upto "hhe" I can figure out what is happening but, where does this "!" came from? ASCII value of "!" is 33 and here I don't think we are anyhow getting this value. Thanks in advance
When you say ch=i++[s]; it means ch=(i++)[s]; , but when you say ch=++i[s]; it means ch=++(i[s]); . It's totally confusing because intuition says it should be ch=(++i)[s]; . But that's where the '!' is coming from. i is 3 at that point, and the next character after ' ' in ASCII is '!'.
Placing the ++ operator before a variable means you would like to increment the value stored in the variable before using it. Placing the ++ after a variable means you would like to use the current value of the variable, then increment it before executing the next line.
int i = 0;
int j = 0;
/*
* This is the same as:
* i = i + 1;
* int k = 0;
*/
int k = ++i; // k will be 1
/*
* This is the same as:
* int l = i;
* i = i + 1;
*/
int l = i++; // l will be 0