can anyone tell the output of following code and also explain it

#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;
}
Member Avatar for I_m_rude

you can't initialize the const variable after its declaration. Declaraing union as constant has no sense because union is bascially used to make it's use again and again. in line 14, you are making changes in a const variable so which is a error here. thnks.

Member Avatar for I_m_rude

yes, it will work as it is undefined behaviour. you are trying to do a thing which is undefined so it is possible that it may work. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.