NVIDIA Interview Question: A C union has an integer and ... | Glassdoor

## Interview Question

Software Engineering Interview

# A C union has an integer and a floating point member. If

the floating point member is assigned the value 1.23, what would be the value in the integer member?
Tags:
c
Answer

## Interview Answer

2 Answers

0

Since all the members of a union share the same memory, the value in the integer depends on the bits that make up 1.23.

Interview Candidate on Feb 26, 2010
6

An integer is usually 32 bits. A double is 64 bits. For a double number, 50 bits or so are used to store the decimal part, 13 bits or so to store the exponent, and one bit for the sign.

Next, one needs to know precisely the order in which these quantities are packed in the 64 bits. Then, the value of the integer in question would be the first 32 bits of this number, interpreted as an integer.

To do this interpretation, one should know if the machine is small endian or big endian, as 32 bits is 4 bytes, and the order in which these 4 bytes are stored for an integer depends on endianess.

That's the idea. A reasonable interviewer would be happy with that hopefully.

Anon on Jul 31, 2011

## Add Answers or Comments

To comment on this, Sign In or Sign Up.