C Programming - Const - Discussion

Discussion Forum : Const - Find Output of Program (Q.No. 2)
2.
What will be the output of the program?
#include<stdio.h>
#include<stdlib.h>

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;
}
Error: RValue required
Error: cannot convert from 'const int *' to 'int *const'
Error: LValue required in strcpy
No error
Answer: Option
Explanation:

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

K 75 0.000000

Discussion:
28 comments Page 1 of 3.

Rishabh said:   1 decade ago
How can 75 be the value of e1.age ?

Div said:   1 decade ago
I would also like to know how e1.age gets the value 75 ?

Nishant sinha said:   1 decade ago
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:   1 decade ago
Please explain in detail.

Maheep Sharma said:   1 decade ago
Why float is not sharing the same memory and not outputting as 75.000000 ?

Archana said:   1 decade ago
Can anyone explain why float doesn't take 75. 000000?

Sona said:   1 decade ago
I think ascii value don't take the float value, so float value is 0.000000.

Rakesh said:   1 decade ago
Please explain in detail.

Nandu said:   1 decade ago
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:   1 decade ago
Since in union memory of highest datatype is allocated then why the memory of float datatype is allocated? why it has allacated 2 bytes?


Post your comments here:

Your comments will be displayed after verification.