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 2 of 3.

Ashi said:   1 decade ago
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?
(1)

Avi said:   1 decade ago
Can we initialize more than one member of union at a time?

Pavel said:   1 decade ago
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.

Anonymous said:   1 decade ago
On GCC compiler, this program gives an error "uninitialized const e1", const enum needs initialization at the time of declaration on gcc compiler.

Anonymous said:   1 decade ago
What is the significance of 'const' here?

Subhchintak said:   1 decade ago
Why has const variable not been defined at the time of declaration.

Avi said:   1 decade ago
How strcpy is being used to store values in a constant variable ?

Deepak said:   1 decade ago
char is of 1 byte.

So char name[15] will take 15 byte.

So this is the max.

So it should allocate 15 bytes.

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?

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.


Post your comments here:

Your comments will be displayed after verification.