C Programming - Memory Allocation - Discussion
Discussion Forum : Memory Allocation - Find Output of Program (Q.No. 4)
4.
What will be the output of the program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
union test
{
int i;
float f;
char c;
};
union test *t;
t = (union test *)malloc(sizeof(union test));
t->f = 10.10f;
printf("%f", t->f);
return 0;
}
Discussion:
22 comments Page 1 of 3.
Baru said:
1 decade ago
Explanation please.
Apple said:
1 decade ago
Hi Baru,
Since t->f=10.10f it gives the output as 10.100000 as it float takes 6 digits after the decimal point.
Since t->f=10.10f it gives the output as 10.100000 as it float takes 6 digits after the decimal point.
Shilpa said:
1 decade ago
Thank you. Apple.
I clarified my doubt, with your answer.
I clarified my doubt, with your answer.
Harsha said:
1 decade ago
Thanks Apple.
Priya said:
1 decade ago
What is the meaning of :
t = (union test *)malloc(sizeof(union test));
t = (union test *)malloc(sizeof(union test));
Nirbhay singh said:
1 decade ago
'union test' is name of user define data type sothat sizeof() operator gives size of varriable used in that datatype and we know that malloc()takes integer argument which tells how many blocks will be created. (union test *) is simple type-casting which will say to compiler that created address is of 'union set' type.
Raju Naidu said:
1 decade ago
@priya
t=(union test*)malloc(sizeof(union test));
Here malloc return different type that's y we've to do type conversion to original type..i.e union test*
Ex int i =5 valid here we r assigning the same type value so int type accepted
but int i=0.23 invalid,here we r assigning the float value it cannot possible so here typecasting is need.
int i=(int)0.23
t=(union test*)malloc(sizeof(union test));
Here malloc return different type that's y we've to do type conversion to original type..i.e union test*
Ex int i =5 valid here we r assigning the same type value so int type accepted
but int i=0.23 invalid,here we r assigning the float value it cannot possible so here typecasting is need.
int i=(int)0.23
The Guide said:
1 decade ago
malloc() returns pointer to void. That is, pointer to the first memory location of the allocated memory block. The pointer type is unknown at this point, hence typecasting is done later.
Vijay said:
1 decade ago
Is it possible to declare, define union in main() ?
Don said:
1 decade ago
free(t) is missing.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers