C Programming - Structures, Unions, Enums - Discussion

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

[A]. Error: scanf() function cannot be used for structures elements.
[B]. The code runs successfully.
[C]. Error: Floating point formats not linked Abnormal program termination.
[D]. Error: structure variable must be initialized.

Answer: Option C

Explanation:

Refer the explanation given for another problem:

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


Gopi said: (Jan 1, 2011)  
Can any one explain this ?

Neo said: (Jul 7, 2013)  
Can anyone please explain the "e[i].name" used in scanf() without '&'?

Vimalmohandas said: (Sep 17, 2013)  
"e[i].name" pointing to address already hence we are not using "&" in scanf().

Neha said: (Mar 13, 2014)  
Using string we don't need there "&" in scanf().

Soumya Sharma said: (Apr 12, 2014)  
Can any one explain me why answer C is correct with suitable example. How we can correctly?

Ratan said: (Sep 29, 2015)  
On deleting last for loop and scanf statements, error remains same, then why these extra codes.

Chintan said: (Dec 13, 2015)  
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 Lal said: (Apr 11, 2016)  
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

Deepa Khandelwal said: (Jul 28, 2016)  
How/ Why the answer is option c?

Robert said: (Aug 23, 2016)  
int a;

scanf ("%d", &a);

Agree with @Ratan In GCC it works smoothly.

Hardik Chugh said: (Oct 25, 2017)  
Run time error occurs in GCC compiler. Can anyone explain this?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.