C Programming - Floating Point Issues - Discussion

Discussion Forum : Floating Point Issues - Point Out Errors (Q.No. 1)
1.
Point out the error in the following program.
#include<stdio.h>
int main()
{
    struct emp
    {
        char name[20];
        float sal;
    };
    struct emp e[10];
    int i;
    for(i=0; i<=9; i++)
        scanf("%s %f", e[i].name, &e[i].sal);
    return 0;
}
Suspicious pointer conversion
Floating point formats not linked (Run time error)
Cannot use scanf() for structures
Strings cannot be nested inside structures
Answer: Option
Explanation:

Compile and Run the above program in Turbo C:

C:\>myprogram.exe
Sundar
2555.50
scanf : floating point formats not linked
Abnormal program termination

The program terminates abnormally at the time of entering the float value for e[i].sal.

Solution:

Just add the following function in your program. It will force the compiler to include required libraries for handling floating point linkages.

static void force_fpf() /* A dummy function */
{
    float x, *y; /* Just declares two variables */
    y = &x;      /* Forces linkage of FP formats */
    x = *y;      /* Suppress warning message about x */
}

Discussion:
36 comments Page 2 of 4.

Pessi said:   1 decade ago
Ok if scanf cannot be used in structures then it would give an error related to it but given a linking error right? what does it mean?

Pathmaraj said:   1 decade ago
I don't know how that function include libraries without calling main(). Can you please explain this concept elaborately?

Consfused said:   9 years ago
See this is a real big problem for us. We are stuck here at our lab because of this we need a solution right now.

Santa said:   1 decade ago
In case of e[i].name it is not showing any error then why in the case of &e[i].sal it is shown the error ?

Audiodroid said:   1 decade ago
Sorry, but this compiles and gives no runtime error on linux, compiling with the latest gcc-compiler.

Maha said:   1 decade ago
I can't understand the answer and why it is done like that?

Can anyone explain me please.

Madhu said:   1 decade ago
Since there no addressing pointer like '&' in scanf. It generates an error.

Divya said:   8 years ago
While I am using float inside a structure, it is fine working in GCC compiler.

ANIK SINGH said:   1 decade ago
I can't understood what you want to say so please define it briefly.

Mani Tej said:   1 decade ago
Can you explain where we can use pointers and why these are need?


Post your comments here:

Your comments will be displayed after verification.