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


void main()
 static char s[25]="The cocaine man";
int i=0;
char ch;


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

Hope that helps!

Edited 4 Years Ago by dmanw100: n/a

you could also try to print out the value of i while incrementing it to see the changes that happens and its current value for yourself

Edited 4 Years Ago by zeroliken: n/a

This question has already been answered. Start a new discussion instead.