Open In App

Output of C++ programs | Set 41 (Structure and Union)

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Structures and Union

QUE.1 What will be the output of this code?




#include <iostream>
using namespace std;
typedef struct s1 {
    int a;
    float b;
    union g1 {
        char c;
        double d;
    };
} new1;
int main()
{
    cout << sizeof(new1);
    return 0;
}


(a)17
(b)16
(c)8
(d)9
Output:

(c) 8

Explanation : In Struct block, it adds the size of float and int so the size is 8 then In union it only declare But not initialize so it gives 8 in the Output.

QUE.2 What will be the output of this Question ?




#include <iostream>
    using namespace std;
    typedef struct s1 {
        int a;
        float b;
        union g1 {
            char c;
            double d;
        } new2;
    } new1;
    int main()
    {
        cout << sizeof(new1);
    }


(a)17
(b)16
(c)8
(d)9
Output:

(b)16

Explanation : In Struct block, it adds the size of float and int so the size is 8 then In union we Define and Declare both in this block so union gives 8 so it gives 16 in the Output.

QUE.3 What will be the output of this question?




#include <iostream>
    using namespace std;
    typedef struct s1 {
        int a;
        float b;
        union g1 {
            double c;
            double d;
        } new2;
    } new1;
    int main()
    {
        cout << sizeof(new1) << endl;
        new1 obj;
        obj.new2.d = 17;
        obj.new2.c = 20;
        cout << obj.new2.d;
        return 0;
    }


(a)17 20
(b)16 20
(c)16 17
(d)24 20
Output:

(b) 16 20

Explanation: In this Question: In union, Block Contain maximum size data type like double so the memory contain 8 byte and when we initialize ‘d’ and ‘c’ then ‘d’ value is replace by ‘c’ because memory blocks are same so when we print ‘d’ and ‘c’ value, it will be print 20 and about size I explained in above examples so it will print 16.

QUE.4 What will be the output of this question?




#include <iostream>
using namespace std;
typedef struct s1 {
    int a;
    float b;
    struct g1 {
        double c;
        double d;
    };
} new1;
int main()
{
    cout << sizeof(new1) << endl;
    return 0;
}


(a)8
(b)16
(c)4
(d)24
Output:

(a)8

Explanation: In Struct block it adds the size of float and int so the size is 8 then In struct g1 block it only declare But not initialize so overall it gives 8 in the Output.

QUE.5 What will be the output of this question?




#include <iostream>
using namespace std;
typedef struct s1 {
    int a;
    float b;
    struct g1 {
        double c;
        double d;
    } new2;
} new1;
int main()
{
    cout << sizeof(new1) << endl;
    return 0;
}


(a)8
(b)16
(c)4
(d)24
Output:

(d)24

Explanation : In Struct block it adds the size of float and int so the size is 8 then In struct g1 block that time it is declared and initialized both so it add size of ‘c’ and ‘d’ so is contain total size 24 so the output Is 24.

Related Articles :



Last Updated : 29 Aug, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads