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

Hemanth said:   8 years ago
int main()
{
static char s[25] = "The cocaine man";
int i=0;
char ch;
ch = s[++I]; //pre-increment pointer from s[0] to s[1] i.e s[1]= h
printf("%c", ch); // print s[1]=h
ch = s[i++]; //post-increment pointer from s[1] to s[2]

printf("%c", ch);// print s[1]=h because ch = s[i++]; is post increment which increment after next print

ch = i++[s]; //post-increment pointer from s[2] to s[3]
printf("%c", ch); // print s[2]=e because ch = s[i++]; is post increment which increment after next print

ch = ++i[s];// this increment s[3] which is blank space by incrementing the ascii address 20 to 21(!)
printf("%c", ch);//print (!)
return 0;
}
(6)

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

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)

Vivek said:   3 years ago
Why it didn't take s[4]=c and gave the answer hhec directly, I mean to say as it did in the first 3 cases, why not it's the same as in the last case. Why do they take ASCII value in the last?
(1)

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)

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

Manohar Reddy said:   1 decade ago
//Remember %c for number means it will print equivalent ASCII value//.

I=0; s[++i] ==> s[1] ==> h.

I=1; s[i++] ==> s[1] ==> h /*Since it is post increment no change to inside i*/.

I=2; i++[s] ==> (i++) [s] ==> s[i++] ==> s[2] ==>e /*post increment to I */.

I=3; ++i[s] ==> ++ (i[s]) ==> ++ (s[i]) ==> ++ (s[3]) ==> ++ (32) ==> 33.

==> ASCII value of 33 !

S[3] is nothing but "space" in the given string. ASCII value for "space" is 32.

It was incremented by 1. So 32+1=33.

ASCII value of 33 will be print.

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

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?


Post your comments here:

Your comments will be displayed after verification.