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

Discussion

What will be the output of the program ?

#include <stdio.h>

int main()
{
    void *vp;
    char ch=74, *cp="JACK";
    int j=65;
    vp=&ch;
    printf("%c", *(char*)vp);
    vp=&j;
    printf("%c", *(int*)vp);
    vp=cp;
    printf("%s", (char*)vp+2);
    return 0;
}

 

  • A.JCK
  • B.J65K
  • C.JAK
  • D.JACK

Answer: D

Correct answer: JACK

Take the three prints in turn. ch is 74, the code for J, so the first prints J. Then j is 65 and *(int*)vp yields 65, which %c prints as A.

Finally vp is set to cp, and (char*)vp+2 advances two characters into "JACK", so %s prints CK. The three outputs concatenate to JACK - a coincidence engineered by the question rather than anything the code copies.

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

Post your comment

Please log in or register to post a comment.