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 4 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 !

FlameNfire said:   1 decade ago
int i=0;
std::cout<<i++;
//Adds 1 to i, but output i's value before adding.

int a=0;
std::cout<<++a;
//Adds 1 to a, but output a's value after adding.(Which seems more logical for us).

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?

Ragini said:   1 decade ago
s[++i] = given i=0 ++i means 0+1 therefore s[1]=h.

s[i++] = s[1] itself because post increment therefore s[1]=h.

i++[s] = s[i+1] as now i=1 increment i => 1+1=2 s[2]=e.

++i[s] = ++(s[i]) => ++s[2] => 33(ascii value ) so !.


Post your comments here:

Your comments will be displayed after verification.