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.
Kani said:
1 decade ago
If anyone please explain the below concept, I can' t understand that:
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!
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!
(3)
Bhawna said:
5 years ago
If we initialize member of the structure like this : e.name = "tiger"; then it is giving garbage value of age. Why? Anyone explain it.
(1)
Amulya said:
6 years ago
I'm not able to understand the program. Please anyone can explain it?
(1)
Palanisuru said:
9 years ago
I am not understand this program, please explain in detail.
(1)
Jemi said:
1 decade ago
#include<stdio.h>
int main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {" ",24,67888};
printf("%d, %f\n", e.name, e.sal);
return 0;
}
Output is :-1086310732, 67888.000000
Why name is in -ve, it should also be 0.
int main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {" ",24,67888};
printf("%d, %f\n", e.name, e.sal);
return 0;
}
Output is :-1086310732, 67888.000000
Why name is in -ve, it should also be 0.
Silambarasan said:
6 years ago
The structure can be initialized inside the main function.
Prajakta said:
7 years ago
This is a local scope so 'why it does not take garbage value' can you please give me the answer?
Asish said:
7 years ago
As per my knowledge, The array is getting garbage values and the other members are getting 0.
How? please explain this concept.
How? please explain this concept.
Sivaramireddy said:
9 years ago
I can't understand the program. Please explain me.
Bhushan patil said:
10 years ago
Is structure is define in main function?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers