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

Discussion

What is the output of this program?

#include <stdio.h>         
void main()
{
struct demo{
    char * a;
    int n;
};

struct demo p = {"hello", 2015};
struct demo q = p;
printf("%d", printf("%s",q.a));
}

 

  • A.hello
  • B.5hello
  • C.hello5
  • D.6hello

Answer: C

Correct answer: hello5

The inner printf runs first, writing hello and returning 5 - the number of characters it wrote. The outer printf then prints that return value. So the output is hello5, in that order. (The assignment q = p copies the structure, including the pointer, so both point at the same string.)

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

Post your comment

Please log in or register to post a comment.