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.

Hariharasubramaniam said:   1 decade ago
s[3] is the space in the string "The cocaine mam" i.e
s[0]==T
s[1]==h
s[2]==e
s[3]==(space) so the ascii value for space was 32,

Since ++i[s] == ++ (i[s]) == ++ (s[i])== ++ (s[3]), so ++(32)==33.

ASCII value 33 will print === !
(1)

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

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?

Ajay said:   9 years ago
Guys! can you tell me the difference between ++i and i++?

Darshan said:   9 years ago
Nice explanation @ Ragini.

Matt said:   9 years ago
++i means that i is incremented first and then evaluated.

i=0;
if(++i == 1) this is evaluated as ((i+1) == 1) which is ((0+1) == 1) which is true.

i++ means that i is evaluated first and then incremented.

i=0;
if(i++ == 1) this is evaluated as (i == 1) which is (0 == 1) which is false and then i = i+1;

Jhon said:   9 years ago
Why s[i]=i[s]?

Tarlika said:   9 years ago
Here for first ch=s[++i]: first i=1,then ch=s[1]='h'.
For second ch=s[i++]:first ch=s[i]=s[1]='h',then i++becomes i=2;.
For third ch=i++[s];ch=2[s]='e';then i++ becomes i=3;.
For fourth ch=++i[s];here first ch=3[s]=s[3]=' '(space), ASSCII value=32 after that ++i[s] Takes place so ch=33 which means ASSCII Value for '!' so ch='!'.
(2)

Koteswararao said:   8 years ago
Nice explanation. Thank you all.

Vinaya said:   8 years ago
How hhe! came? Please explain me.
(2)


Post your comments here:

Your comments will be displayed after verification.