C Programming - Const - Discussion

Discussion Forum : Const - Point Out Errors (Q.No. 3)
3.
Point out the error in 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", e1.name);    
    e1.age=85;
    printf("%d", e1.age);
    printf("%f", e1.salary);
    return 0;
}
Error: RValue required
Error: cannot modify const object
Error: LValue required in strcpy
No error
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
11 comments Page 1 of 2.

Dhavaprathap said:   1 decade ago
For this problem , the first error what i have encountered is " Uninitialized constant Object " at the line

const union employee e1;

Jessie said:   1 decade ago
I cant understand. please anyone help me...

NITESH KHATRI said:   1 decade ago
Because its union in union different variable stores at same location, So in program the value supplied to char name[] variable will be stored at location which will be shared by all other union members so the ASCII value of 'k' 75 will be stored, and 'int age' variable also will be having 75 because the memory location of 'char name[]' & 'int age' is same, as we declared union as constant so if you try to change the value 75 to 85 it will give an error.
(1)

Shwetha said:   1 decade ago
A const variable must be initialized during the declaration itself.

Venkatesh said:   1 decade ago
#include<stdio.h>
#include<stdlib.h>

union employee
{
char name[15];
int age;
float salary;
};
union employee e1;

int main()
{
strcpy(e1.name, "K");
printf("%s\n", e1.name);
e1.age=85;
printf("%d\n", e1.age);
printf("%f", e1.salary);
return 0;
}

Now the above program is giving output as:
K
85
0.000000

Why 0.000000 is coming?

James said:   1 decade ago
@Venkatesh:

You have not initialized salary variable.

By default compiler will initialize variable according to type.

As salary is float, it it initialized as 0.000000.

Anil s dali said:   10 years ago
@Venkatesh:

The value stored in e.age is integer type, so if we want to print integer variable into float variable in printf statement, it is automatically initialized to 0.

Anil s dlai said:   10 years ago
@Venkatesh.

Even though if we initialized e.salary to a number we get 0 as output, this is because we initialized e.age as integer type, so if we want to print e.salary (float type) we get 0.000000 as output.

Compile this:

#include<stdio.h>
#include<stdlib.h>

union employee
{
char name[15];
int age;
float salary;
};
union employee e1;

int main()
{
strcpy(e1.name, "K");
printf("%s\n", e1.name);
e1.salary=95;
e1.age=85;
printf("%f", e1.salary);
return 0;
}

Karthik Prasad said:   10 years ago
Why doesn't that give error as the const variable can't be modified, it does give error in other data types constant but why not here ?

Tejas hegde said:   8 years ago
@ALL.

#include<stdlib.h>

union employee
{
char name[15];
int age;
float salary;
};
const union employee e1;

int main()
{
e1.age=18;
printf("%s", e1.name);
printf("%d", e1.age);
printf("%f", e1.salary);
return 0;
}

Why does it take name but not age?

#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", e1.name);
printf("%d", e1.age);
printf("%f", e1.salary);
return 0;
}

This is valid.


Post your comments here:

Your comments will be displayed after verification.