C Programming - Const - Discussion

Discussion :: Const - Find Output of Program (Q.No.2)


What will be the output of the program?


union employee
    char name[15];
    int age;
    float salary;
const union employee e1;

int main()
    strcpy(e1.name, "K");
    printf("%s %d %f", e1.name, e1.age, e1.salary);
    return 0;

[A]. Error: RValue required
[B]. Error: cannot convert from 'const int *' to 'int *const'
[C]. Error: LValue required in strcpy
[D]. No error

Answer: Option D


The output will be (in 16-bit platform DOS):

K 75 0.000000

Rishabh said: (Nov 10, 2010)  
How can 75 be the value of e1.age ?

Div said: (Dec 6, 2010)  
I would also like to know how e1.age gets the value 75 ?

Nishant Sinha said: (Jan 24, 2011)  
Since union makes use of shared memory. So it allocates the memory for datatype which will take highest memory i.e int. So it will allocate 16 bit in turbo c++ and 32 bits in unix compiler. So the character 'k' will be stored in memory as the bit value of 75 as ascii value of k is 75. Since the int is also sharing this memory so it will read the same memory content with a padding of some zeros to make it 2 bytes in turboo or 4 bytes in unix.

Ajay said: (Feb 16, 2011)  
Please explain in detail.

Maheep Sharma said: (Feb 16, 2011)  
Why float is not sharing the same memory and not outputting as 75.000000 ?

Archana said: (Sep 20, 2011)  
Can anyone explain why float doesn't take 75. 000000?

Sona said: (Nov 17, 2011)  
I think ascii value don't take the float value, so float value is 0.000000.

Rakesh said: (Dec 9, 2011)  
Please explain in detail.

Nandu said: (Dec 27, 2011)  
The ascii value of K=75, ascii deosnt take float value so float value z 0.000000 thats it. Once see ascii table it will be easy for you guys.

Asha said: (Feb 3, 2012)  
Since in union memory of highest datatype is allocated then why the memory of float datatype is allocated? why it has allacated 2 bytes?

Deepak said: (Mar 29, 2012)  
char is of 1 byte.

So char name[15] will take 15 byte.

So this is the max.

So it should allocate 15 bytes.

Avi said: (Jul 19, 2012)  
How strcpy is being used to store values in a constant variable ?

Subhchintak said: (Jul 1, 2013)  
Why has const variable not been defined at the time of declaration.

Anonymous said: (Aug 24, 2013)  
What is the significance of 'const' here?

Anonymous said: (Aug 30, 2013)  
On GCC compiler, this program gives an error "uninitialized const e1", const enum needs initialization at the time of declaration on gcc compiler.

Pavel said: (Oct 26, 2013)  
All global or static variables that are not initialized, automatically initialized to 0.

Union e1 will take 15 bytes according to its largest element (char name[15]).

Before main function execution e1 contains 15 zero bytes.

strcpy(e1.name, "K"); - copy 75(ASCII of 'K') and 00('\0' end of string) to e1. After this line e1 = 75 00 00 ... 00

It is easy to see that e1.name = "K".

e1.age = 75 00 or 75 00 00 00 depending on the int size (16 bits or 32 bits). For little endian machine (like x86 machine) both equals to 75.

e1.float = 75 00 00 00.

For little endian machines e1.float = 0|00000000|00000000000000001001011 (float usually takes 4 bytes).

Sign bit (first bit from left) = 0
Exponent (bits 2-9 from left) = 0, which reserved for de-normalized numbers.

Mantissa (bits 10-32): 2^(-17)+2^(-20)+2^(-22)+2^(-23)

For denormalized floating numbers:
So e1.float=(-1)^(sign)*2^(-126)*(Mantissa) = 2^(-126)*( 2^(-17)+2^(-20)+2^(-22)+2^(-23)) = 1.050974e-043.

When you print it with %f, the value rounded it 0.000000.

You can see the precise value by changing %f to %e at printf function.

Avi said: (Nov 7, 2013)  
Can we initialize more than one member of union at a time?

Ashi said: (Jul 7, 2014)  
How can we assign a value to a const union member by strcpy? Is it possible to assign a const variable a value apart from the declaration?

Sunny said: (Aug 9, 2014)  
How can one modify const variable at runtime. The given answer is wrong better you guys tried it out with your favourite compiler & then start debate.
Answer - B.

Please correct me if I am wrong.

Akshit said: (Nov 29, 2014)  
The answer is correct (D). The explanation made by Pavel is Correct. Checked it out with my compiler.

Anurag said: (Jun 26, 2015)  
What is the use of const here?

Tejaswi said: (Jun 27, 2015)  
Const is used how can we change the values?

Vinay said: (Dec 6, 2015)  
It create object for the employee class then call all name and age and salary with the help of e1.name, e1.salary...

Varunrao.Rao@Gmail.Com said: (May 21, 2016)  
Here string copy function is used. It updates the union using reference.

So, no error is correct.

Dinesh Prajapat said: (Aug 11, 2017)  
Output is K 0 0.000000.

Suma said: (Sep 9, 2019)  
We are not changing the values here,just printing 'K' as different members of e1. If we try to assign e1.age or e1.salary, then it is an error, displaying assignment to a const variable.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.