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

Prerequisite: Structure and Union

QUE.1 What is the output of this program ?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
struct sample {
    int a = 0;
    char b = 'A';
    float c = 10.5;
};
int main()
{
    struct sample s;
    printf("%d, %c, %f", s.a, s.b, s.c);
    return 0;
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    struct bitfield {
        signed int a : 3;
        unsigned int b : 13;
        unsigned int c : 1;
    };
    struct bitfield bit1 = { 2, 14, 1 };
    printf("%d", sizeof(bit1));
    return 0;
}

chevron_right


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 the will be int and size of int is 4

QUE. 3 What is the output of this program ?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

This article is contributed by Ajay Puri(ajay0007). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up