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;
}
Discussion:
11 comments Page 1 of 2.
Ahmed hashem said:
2 years ago
I think in this case e1.name is initialized by garbage values, e1.age/salary initialized by 0.
So, we can not modify any one of them, but by using strcpy function we can modify e1.name indirectly by using a pointer and this is valid.
strcpy function :
char* strcpy(char* destination, const char* source);
So, we can not modify any one of them, but by using strcpy function we can modify e1.name indirectly by using a pointer and this is valid.
strcpy function :
char* strcpy(char* destination, const char* source);
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.
#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.
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 ?
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;
}
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;
}
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.
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.
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.
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.
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?
#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?
Shwetha said:
1 decade ago
A const variable must be initialized during the declaration itself.
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)
Jessie said:
1 decade ago
I cant understand. please anyone help me...
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers