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 1 of 4.

Naveen said:   1 decade ago
can u pls explain it elaborately ???

Manohar said:   1 decade ago
Can scanf() be used to read values for multiple variables ?

Ashok said:   1 decade ago
Ya scanf read the value.

Badhri said:   1 decade ago
@naveen : use a dummy variable initially to get input. Then substitute it to the structure float variable.
Program :
#include<stdio.h>
int main()
{
struct emp
{
char name[20];
float sal;
};
struct emp e[10];
int i;
float dummy;
for(i=0; i<=9; i++)
scanf("%f",&dummy);
e[i].sal = dummy;
scanf("%s", e[i].name);
return 0;
}

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

Ajeet said:   1 decade ago
Try including <float.h>

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

Sri said:   1 decade ago
Is it Impossible to use scanf in structrues with array ?

Jay said:   1 decade ago
Can you tell me what is floating point format linking?

Pawan Agnihotri said:   1 decade ago
Can you tell me what is floating point format linking?


Post your comments here:

Your comments will be displayed after verification.