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 3 of 3.
Anurag said:
1 decade ago
What is the use of const here?
Tejaswi said:
1 decade ago
Const is used how can we change the values?
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...
Varunrao.rao@gmail.com said:
9 years ago
Here string copy function is used. It updates the union using reference.
So, no error is correct.
So, no error is correct.
Dinesh Prajapat said:
8 years ago
Output is K 0 0.000000.
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.
Jyothi said:
3 years ago
Please explain in detail.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers