C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 13)
13.
What will be the output of the program ?
#include<stdio.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", ch);
    return 0;
}
hhe!
he c
The c
Hhec
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
34 comments Page 3 of 4.

Shashi said:   1 decade ago
[] has higher precedence than ++.
so i++[s] <---> (i++)[s] <---> s[i++]
++i[s] <---> ++(i[s]) <---> ++s[i]=33 whose ascii is !

Xyz said:   1 decade ago
Can anyone explain clearly.

Iranna said:   1 decade ago
s[++i]=s[1]=h here i is incremented and assigned
s[i++]=s[1]=h here i is incremented but not assigned
i++[s]<->(i++)[s]<->s[i++]=s[2]=e
++i[s]<->++(i[s])<->++(s[i])=++(s[3])=++(asci value of space)=++(32)=33 is equivalent to !

So ans is hhe!

Madhu said:   1 decade ago
The o/p is hhe! because ch=++i[s];

Firstly i is value is 3 from i++ then we use ++i[s]

++i is evaluated its space ascii value is incremented and it becomes !.

So the o/p is hhe!

Prathima said:   1 decade ago
Here in last hhe! bcoz....ch=++i[s];
becomes ch=++(spacecharacter);
ch=++32
ch=33
in printf prints d ascii character of 33.

KHUSHBOO said:   1 decade ago
I don't understand it can anyone explain it clearly.

Midhu said:   1 decade ago
++i[s] <-> ++(i[s]) <-> ++(s[i]);

is it possible?

Uday said:   1 decade ago
In frist printf statement how 'ch' is get the value 104?

Pankaj said:   1 decade ago
i++[s] is equivalent to s[i]; i=i+1;

++i[s] is equivalent to ++(s[i]);

Pallavi said:   1 decade ago
Can anyone explain the difference between i++[s] and ++i[s] ?


Post your comments here:

Your comments will be displayed after verification.