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.
Krish bhanderi said:
1 year 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.
Jyothi said:
2 years ago
Please explain in detail.
Suma said:
5 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:
7 years ago
Output is K 0 0.000000.
Varunrao.rao@gmail.com said:
8 years ago
Here string copy function is used. It updates the union using reference.
So, no error is correct.
So, no error is correct.
Vinay said:
9 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:
9 years ago
Const is used how can we change the values?
Anurag said:
9 years ago
What is the use of const here?
Akshit said:
10 years ago
The answer is correct (D). The explanation made by Pavel is Correct. Checked it out with my compiler.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers