While looking at some conceptual questions in C,I came across this question in a book.
What is the output of the following program ?
#include<stdio.h>
#include<string.h>
int main()
{
static char s[25]="The cocaine man";
int i=0;
char ch;
ch=s[++i];
printf("%c",ch);
ch=s[i++];
printf("%c",ch);
ch=i++[s];
printf("%c",ch);
ch= ++i[s];
printf("%c\n",ch);
return 0;
}
Answer :
hhe!
Can anyone please explain how this output came ?
Starting from the first assignment
ch=s[++i];increments i(i=1) and assigns
chthe character at index (i=1) ofs.ch=s[i++];assigns
chthe character at index (i=1) ofsand then increments i(i=2).ch=i++[s];assigns
chthe character at index (i=2) ofsand then increments i(i=3). Key:s[i] == i[s].ch= ++i[s];increments the ASCII value at index (i=3) of
sand assigns it to ch. Key :[]has higher precedence than prefix++