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.

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;

Ravli said:   1 decade ago
I didn't understand it clearly can anyone explain it?

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

Richa said:   7 years ago
++i is pre incrementor.

Whereas i++ is postincrementor, both of these are the binary operator and has LOW PRIORITY when compared to Assignment operator(=),so in 1st case, ++i yields s[1], and then due to priority case first " VALUE IS ASSIGNED AND THEN INCREMENTED".

Chetan said:   3 years ago
Thanks for the explanation @Tarlika.

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.

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 '!'.

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


Post your comments here:

Your comments will be displayed after verification.