C Programming - Const - Discussion

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

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

K 75 0.000000

28 comments Page 1 of 3.

Krish bhanderi said:   5 months ago

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.

Jyothi said:   1 year ago
Please explain in detail.

Suma said:   4 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.

Dinesh Prajapat said:   6 years ago
Output is K 0 0.000000.

Varunrao.rao@gmail.com said:   7 years ago
Here string copy function is used. It updates the union using reference.

So, no error is correct.

Vinay said:   8 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...

Tejaswi said:   8 years ago
Const is used how can we change the values?

Anurag said:   8 years ago
What is the use of const here?

Akshit said:   9 years ago
The answer is correct (D). The explanation made by Pavel is Correct. Checked it out with my compiler.

Sunny said:   9 years 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.

Post your comments here:

Your comments will be displayed after verification.