Machine-translated by Google. Questions are authored in English.
Top

Discussion

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;
}

 

  • A.hhe!
  • B.he c
  • C.The c
  • D.Hhec

Answer: A

Correct answer: hhe!

Take the four statements in turn, with s = "The cocaine man" and i = 0.

  • s[++i] - i becomes 1 first, so this is s[1] = h.
  • s[i++] - the old i, still 1, so h again; i becomes 2.
  • i++[s] - the same as s[i++], so s[2] = e; i becomes 3.
  • ++i[s] - subscript binds tighter than the prefix ++, so this increments the character s[3], a space (32), to 33 = !.

Output: hhe!

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.