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.
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)
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.
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.
Jyothi said:
3 years ago
Please explain in detail.
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.
Dinesh Prajapat said:
8 years ago
Output is K 0 0.000000.
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.
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...
Tejaswi said:
1 decade ago
Const is used how can we change the values?
Anurag said:
1 decade ago
What is the use of const here?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers