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;
}
Answer: Option
Explanation:
The output will be (in 16-bit platform DOS):
K 75 0.000000
Discussion:
28 comments Page 1 of 3.
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.
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.
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.
Krish bhanderi said:
2 years ago
@All.
Here is my explanation.
Yes, strcpy() expect char * in his first parameter but we provide const char * so the compiler throws the warning.
---> warning: incompatible implicit declaration of built-in function ‘strcpy’
Either provided answer is right because the compiler is not throwing any error but neither provides any output.
Here is my explanation.
Yes, strcpy() expect char * in his first parameter but we provide const char * so the compiler throws the warning.
---> warning: incompatible implicit declaration of built-in function ‘strcpy’
Either provided answer is right because the compiler is not throwing any error but neither provides any output.
Sunny said:
1 decade ago
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.
Answer - B.
Please correct me if I am wrong.
Suma said:
6 years ago
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.
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.
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.
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)
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?
Vinay said:
10 years ago
It create object for the employee class then call all name and age and salary with the help of e1.name, e1.salary...
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers