C Programming - Declarations and Initializations - Discussion
Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 5)
5.
What is the output of the program
#include<stdio.h>
int main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {"Tiger"};
printf("%d, %f\n", e.age, e.sal);
return 0;
}
Answer: Option
Explanation:
When an automatic structure is partially initialized remaining elements are initialized to 0(zero).
Discussion:
30 comments Page 1 of 3.
Bhuvana said:
1 decade ago
I can't understand where structure is partially initialized.
Could you please explain the concept please ?.
Could you please explain the concept please ?.
Joe Mac said:
1 decade ago
Hi Bhuvana, the structure is initialized in the statement
struct emp e = {"Tiger"};
So all other structure members are initialized to null. Note that initialization order should be same as the member definition order.
struct emp e = {"Tiger"};
So all other structure members are initialized to null. Note that initialization order should be same as the member definition order.
Arya said:
1 decade ago
If I remove the char initialization, the output will be changed why?
Can you please give me the answer please.
Can you please give me the answer please.
Sandeep said:
1 decade ago
In the question tiger is being assigned to the char array ony. how can you remove that initialization??
Sundar said:
1 decade ago
If we use: printf("%s %d, %f\n", e.name, e.age, e.sal);
struct emp e = {"Tiger", 5, 1500.50}; // Full Initialization
//Ouput: Tiger, 25, 1500.500000
struct emp e = {"Tiger"}; // Partial Initialization
//Ouput: Tiger, 0, 0.000000
struct emp e ; // No Initialization
//Ouput: GarbageValue, GarbageValue, GarbageValue!
I hope this help you. Have a nice day
struct emp e = {"Tiger", 5, 1500.50}; // Full Initialization
//Ouput: Tiger, 25, 1500.500000
struct emp e = {"Tiger"}; // Partial Initialization
//Ouput: Tiger, 0, 0.000000
struct emp e ; // No Initialization
//Ouput: GarbageValue, GarbageValue, GarbageValue!
I hope this help you. Have a nice day
Gangadhararao said:
1 decade ago
#include<stdio.h>
int main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {"Tiger"};
printf("%d, %f\n", e.age, e.sal);
return 0;
}
int main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {"Tiger"};
printf("%d, %f\n", e.age, e.sal);
return 0;
}
Ram krishna said:
1 decade ago
Thanks to sundar.
Kavita.C.Karjagar said:
1 decade ago
I can't understand plz explain in dept.
Ashok ims said:
1 decade ago
Sundar ji your explation is really well, but you commented that if not initialize emp its output garvage value but such as not and one more prablem tiger is not print also when write in printf fuction why?
Please run the proram and explain again it.
Please run the proram and explain again it.
Shiwam pandey said:
1 decade ago
Is it not take default values when any structure defined partialy.
Element of structure should take default values.
Element of structure should take default values.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers