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

Discussion

What will be the output of the following C code?

#include <stdio.h>
    typedef struct student
    {
        char *a;
    }stu;
    void main()
    {
        struct student s;
        s.a = "hey";
        printf("%s", s.a);
    }

 

  • A.Compile time error
  • B.Varies
  • C.he
  • D.hey

Answer: D

Correct answer: hey

Here the declaration uses the tag rather than the alias - struct student s; - and the tag student does exist, so this compiles. The member a is a char *, assigning a string literal to it is legal, and %s prints it: the output is hey. Compare the previous question, which used struct stu and failed for exactly this reason.

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

Post your comment

Please log in or register to post a comment.