C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 2)
2.
What will be the output of the program ?
#include<stdio.h>
int main()
{
union var
{
int a, b;
};
union var v;
v.a=10;
v.b=20;
printf("%d\n", v.a);
return 0;
}
Discussion:
83 comments Page 8 of 9.
Prashanth said:
1 decade ago
David Rajesh your explanation is good
Srihari said:
1 decade ago
Union allocate memory for the variables which has highest.
Size only once, and all other variables are over write in it.
Hence the above program memory for int allocate once, first it store 'a' variable value later it over write with recent initialize 'b'.
Value, so the out put will be 'b' value.
Size only once, and all other variables are over write in it.
Hence the above program memory for int allocate once, first it store 'a' variable value later it over write with recent initialize 'b'.
Value, so the out put will be 'b' value.
Jkhjkh said:
1 decade ago
Here we came to know that, union allocates memory of 2 bytes for an integer.
In 1st prg we have assigned as 20 and in 2nd prg we have assigned as 10. i.e., when you print the address of both we. A and we. B the address are same, because union allocates the largest memory size for 2bytes only.
In 1st prg we have assigned as 20 and in 2nd prg we have assigned as 10. i.e., when you print the address of both we. A and we. B the address are same, because union allocates the largest memory size for 2bytes only.
Shobana said:
2 decades ago
How it will print 20?
Mohamed rhiyassuddin said:
1 decade ago
#include<stdio.h>
int main()
{
union var
{
int a, b;
};
union var v;
//v.a=10;
v.b=20;
printf("%d\n", v.a);
return 0;
}
output for the above program is :20.
can u guess? v.a is not assigned any value instead the value is printed...
int main()
{
union var
{
int a, b;
};
union var v;
//v.a=10;
v.b=20;
printf("%d\n", v.a);
return 0;
}
output for the above program is :20.
can u guess? v.a is not assigned any value instead the value is printed...
Sss said:
1 decade ago
Good Explanation...
Bips said:
1 decade ago
I think it will give compile error...
cause
v.a=10;
v.b=20;
printf("%d\n", v.a);
since v.b=20 overwrites the value v.a=10
but program is trying to print the value which doesnot exist.
...
..
.
correct me if i m wrong...
cause
v.a=10;
v.b=20;
printf("%d\n", v.a);
since v.b=20 overwrites the value v.a=10
but program is trying to print the value which doesnot exist.
...
..
.
correct me if i m wrong...
Sai said:
1 decade ago
The difference between union and structure is
Structure used as prototype in creating objects with same memory, it can not compress the memory.
struct a{ int a; float b; }obj;
Here obj is having 6 bytes in memory.
Where as for union, it allocates largest memory among all datatypes defined in the union.
union a{ int a; float b;}obj;
Here obj is having 4 bytes instead of 6 Bytes.
So, union utilizes largest memory assigned to variable among all variables and it will be used for all its operations.
Structure used as prototype in creating objects with same memory, it can not compress the memory.
struct a{ int a; float b; }obj;
Here obj is having 6 bytes in memory.
Where as for union, it allocates largest memory among all datatypes defined in the union.
union a{ int a; float b;}obj;
Here obj is having 4 bytes instead of 6 Bytes.
So, union utilizes largest memory assigned to variable among all variables and it will be used for all its operations.
Smit said:
1 decade ago
Good Logic Bro.
But it also in structure?
But it also in structure?
Abhay said:
1 decade ago
If in above question a is initiallised with 20 and b with 10 then
o/p becomes 10(the last updated value is 10)..... ..!!!!
o/p becomes 10(the last updated value is 10)..... ..!!!!
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers