Open In App

Output of C programs | Set 44 (Structure & Union)

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Structure and Union
QUE.1 What is the output of this program? 

C




#include <stdio.h>
int main()
{
    typedef struct tag {
        char str[10];
        int a;
    } har;
 
    har h1, h2 = { "IHelp", 10 };
    h1 = h2;
    h1.str[1] = 'h';
    printf("%s, %d", h1.str, h1.a);
    return 0;
}


OPTION 
a) Error 
b) 0, A, 10.5 
c) 0, A, 10.500000 
d) No Error, No Output 

Answer: a

Explanation: Error: Can not initialize members here. We can only declare members inside the structure, initialization of member with declaration is not allowed in structure declaration.
QUE.2 What is the output of this program? 

C




#include <stdio.h>
 
struct sample {
    int a;
} sample;
 
int main()
{
    sample.a = 100;
    printf("%d", sample.a);
    return 0;
}


OPTION 
a) 4 
b) 6 
c) 8 
d) 12 

Answer:  a

Explanation: struct bitfield bit1={2, 14, 1}; when we initialize it, it will take only one value that will be int and size of int is 4
QUE. 3 What is the output of this program? 

C




#include <stdio.h>
int main()
{
    union test {
        int i;
        int j;
    };
 
    union test var = 10;
    printf("%d, %d\n", var.i, var.j);
}


OPTION 
a) ERROR 
b) IHelp, 10 
c) IHelp, 0 
d) Ihelp, 10 

Answer : d

Explanation: It is possible to copy one structure variable into another like h1 = h2. Hence value of h2. str is assigned to h1.str.
QUE.4 What is the output? 

C





OPTION 
a) 0 
b) 100 
c) ERROR 
d) Warning 
Answer 

Answer : b

Explanation : This type of declaration is allowed in c.
QUE.5 what is output of this program? 

C




#include <stdio.h>
int main()
{
    union test {
        int i;
        int j;
    };
 
    union test var = 10;
    printf("%d, %d\n", var.i, var.j);
}


OPTION 
a) 10, 10 
b) 10, 0 
c) 0, 10 
d) Compile Error

Answer : d

Explanation : Error: Invalid Initialization. You cannot initialize an union variable like this.
Next Quiz onStructure and Union



Last Updated : 25 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads