C Programming - Const - Discussion

Discussion :: 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;
}

[A]. Error: RValue required
[B]. Error: cannot modify const object
[C]. Error: LValue required in strcpy
[D]. No error

Answer: Option B

Explanation:

No answer description available for this question.

Dhavaprathap said: (Aug 16, 2010)  
For this problem , the first error what i have encountered is " Uninitialized constant Object " at the line

const union employee e1;

Jessie said: (Aug 31, 2012)  
I cant understand. please anyone help me...

Nitesh Khatri said: (Oct 20, 2012)  
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.

Shwetha said: (Sep 14, 2013)  
A const variable must be initialized during the declaration itself.

Venkatesh said: (Dec 1, 2014)  
#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: (May 24, 2015)  
@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: (Aug 17, 2015)  
@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: (Aug 17, 2015)  
@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: (Jan 23, 2016)  
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: (Jun 20, 2017)  
@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:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.