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 2 of 4.

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!

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 !

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.

Preethi said:   1 decade ago
What is the actual explanation. Please explain it clearly for each line. What is ASCII value. How you are calculating it. Tell me clearly?

Sumit said:   1 decade ago
Because here we are incrementing the value of s[4]. Before its space that is having ascii value 48 after incrementing it ll become '!'.

Ameena said:   10 years ago
What is the difference between static and char?

Here if we don't mention static what happened in program can any one explain?

Anusha.v said:   1 decade ago
In the last statement ++i[s] is 33. I didn't understand explanation of this statement.

Devnath said:   1 decade ago
Replace 48 by 32, because white space ascii value is 32, and 33 is for "!".

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

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

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

is it possible?


Post your comments here:

Your comments will be displayed after verification.