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;
}
0, 0.000000
Garbage values
Error
None of above
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!
(3)

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

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.

Sai said:   1 decade ago
String contains null character in it i.e "\0" is same as " ".

String takes the memory, it has given the address value of the null character stored.

For example if you have "tiger" which also have null character in it. i.e "tiger\0"

Any string at the end contains null.

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.

Prasanna said:   1 decade ago
@Mr.Sivaraman.

struct emp means defining a structure with the name emp.

Structure is a collection of heterogeneous elements. Here the structure contains 3 variables name (it is an array), age and salary. Here e is the object to the structure.

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;
}

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.

Krishna said:   1 decade ago
If we not give size of name than error will come
"In function 'main':
Line 6: error: flexible array member not at end of struct"
why?

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)


Post your comments here:

Your comments will be displayed after verification.