Awesome q2a theme
0 votes
32 views

Please explain the solution along with the concept.

in Programming by (734 points) | 32 views

1 Answer

0 votes

Please read: https://www.geeksforgeeks.org/union-c/ Then look up Unions in a standard textbook.

The memory allocated to a union will be equal to the size of the largest data type it contains. In this case, that data type is int.

For this question - the sizeof(abc) will be the same as sizeof(int). And that could be 2 or 4 (or otherwise) depending on the platform of execution. Such questions are usually ambiguous because the size of any data type is ultimately dependent on the underlying platform.

by (1.9k points)
0

The memory allocated to a union will be equal to the size of the largest data type it contains. In this case, that data type is int.

I am not sure about whether it will take the size of the largest data type or collectively largest space taken by all the variable of that particular data type (i.e for all the char variable collectively)?

0
You're thinking about a structure. Read up on the behavior of a union. And also simply execute the given program to make sure..
0
let me clear the things up

structure will take the space collectively by int and char variables both i.e. sizeof(int)+sizeof(char)

union will take the space taken by the largest data type i.e. sizeof(int)

right?

this might be silly but please make it clear -

I thought union will take the size equal to the sum of the size of all characters means there are 8 characters (a,b,c,d,e,f,g,h) and each character will take 1B so total bytes allocated will be 8B , this is not true, right? all charecters will reside in the 1B space.
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Welcome to GATE CSE Doubts, where you can ask questions and receive answers from other members of the community.
Top Users Apr 2020
  1. !KARAN

    36 Points

  2. lazygatestudent

    5 Points

  3. AngeloAlba89

    5 Points

  4. WillKnisley

    5 Points

  5. TamieMcvay4

    5 Points

  6. Satyaki Das

    5 Points

  7. akansha03

    5 Points

  8. srestha

    3 Points

  9. Shivateja MST

    2 Points

  10. souren

    2 Points

Monthly Top User and those within 60% of his/her points will get a share of monthly revenue of GO subject to a minimum payout of Rs. 500. Current monthly budget for Top Users is Rs. 75.
3,511 questions
1,656 answers
10,449 comments
90,037 users