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

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 ?

Ankit said:   1 decade ago
Please help me. Is it because of & not there before e[i].

JjB said:   1 decade ago
e[i].name refers to the address so no need of &.

Pramod mohite said:   1 decade ago
Why the problem use the float value use in structure?

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

Kunal said:   1 decade ago
No sir in gcc you can also link the float variable.

main(){
struct emp{

float k;
};
struct emp e;
printf("enter multiple vallues");
scanf("%f",&e.k);
printf("%f",e.k);

}

Try it in gcc.

Kunal said:   1 decade ago
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++){
printf("%s %d %f", e[i].name, e[i].age, e[i].sal);
}
}

Try it also. It also work then what is the problem?

Kalaivanan said:   1 decade ago
Floating point not linked abnormal program termination error.

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

Can anyone explain me please.

Karthik said:   10 years ago
This is because Turbo and Borland C/ C++ compilers sometimes leave out floating point support and use non-floating-point version of printf and scanf to save space on smaller systems.

The dummy call to a floating-point function will force the compiler to load the floating-point support and solve the original problem.


Post your comments here:

Your comments will be displayed after verification.