C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Point Out Correct Statements (Q.No. 1)
1.
Which of the following statements correct about the below program?
#include<stdio.h>

int main()
{
    struct emp
    {
        char name[25];
        int age;
        float sal;
    };
    struct emp e[2];
    int i=0;
    for(i=0; i<2; i++)
        scanf("%s %d %f", e[i].name, &e[i].age, &e[i].sal);

    for(i=0; i<2; i++)
        scanf("%s %d %f", e[i].name, e[i].age, e[i].sal);
    return 0;
}
Error: scanf() function cannot be used for structures elements.
The code runs successfully.
Error: Floating point formats not linked Abnormal program termination.
Error: structure variable must be initialized.
Answer: Option
Explanation:

Refer the explanation given for another problem:

http://www.indiabix.com/c-programming/floating-point-issues/discussion-136

Discussion:
11 comments Page 1 of 2.

Hardik chugh said:   8 years ago
Run time error occurs in GCC compiler. Can anyone explain this?

Robert said:   9 years ago
int a;

scanf ("%d", &a);

Agree with @Ratan In GCC it works smoothly.

Deepa khandelwal said:   9 years ago
How/ Why the answer is option c?

Ratan Lal said:   9 years ago
This will run correctly in GCC, if we will apply the address operator to e[i].age and e[i].sal in second scanf statement .

e[i].age should be &e[i].age
e[i].sal should be &e[i].sal

Chintan said:   10 years ago
1 > not floating point error in gcc.

2 > in second for loop the scanf() not write &.

scanf ("%s %d %f", e[i].name, e[i].age, e[i].sal);

So compile successfully but give runtime error solution.

Ratan said:   10 years ago
On deleting last for loop and scanf statements, error remains same, then why these extra codes.

Soumya sharma said:   1 decade ago
Can any one explain me why answer C is correct with suitable example. How we can correctly?

Neha said:   1 decade ago
Using string we don't need there "&" in scanf().

Vimalmohandas said:   1 decade ago
"e[i].name" pointing to address already hence we are not using "&" in scanf().

Neo said:   1 decade ago
Can anyone please explain the "e[i].name" used in scanf() without '&'?


Post your comments here:

Your comments will be displayed after verification.