C Programming - Floating Point Issues - Discussion

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

[A]. Suspicious pointer conversion
[B]. Floating point formats not linked (Run time error)
[C]. Cannot use scanf() for structures
[D]. Strings cannot be nested inside structures

Answer: Option B

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 */
}


Naveen said: (Apr 22, 2011)  
can u pls explain it elaborately ???

Manohar said: (May 30, 2011)  
Can scanf() be used to read values for multiple variables ?

Ashok said: (Jun 16, 2011)  
Ya scanf read the value.

Badhri said: (Jun 27, 2011)  
@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: (Jul 3, 2011)  
Sorry, but this compiles and gives no runtime error on linux, compiling with the latest gcc-compiler.

Ajeet said: (Oct 2, 2011)  
Try including <float.h>

Anik Singh said: (Nov 12, 2011)  
I can't understood what you want to say so please define it briefly.

Sri said: (Jan 17, 2012)  
Is it Impossible to use scanf in structrues with array ?

Jay said: (Mar 18, 2012)  
Can you tell me what is floating point format linking?

Pawan Agnihotri said: (May 4, 2012)  
Can you tell me what is floating point format linking?

Sagr said: (Jul 19, 2012)  
When the compiler encounters a reference to the address of float, it sets a flag to have the linker link in the floating point emulator. There are some cases in which the reference to a float is a bit obscure and the compiler does not detect the need for emulator.

Mani Tej said: (Sep 14, 2012)  
Can you explain where we can use pointers and why these are need?

Kirti said: (Jan 20, 2013)  
What is emulator? What it does?

Pessi said: (Mar 3, 2013)  
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?

Vikas Tyagi said: (Apr 10, 2013)  
/* Usage of an array of structures */
#include<stdio.h>

void main( )
{
struct book
{
char name[10] ;
float price ;
int pages ;
} ;
struct book b[100] ;
int i ;
float temp;
for ( i = 0 ; i <=99; i++ )
{
printf ( "\nEnter name, price and pages " ) ;
scanf ( "%s %f %d", &b[i].name, &temp, &b[i].pages ) ;
b[i].price=temp;
}
for ( i = 0 ; i <= 99 ; i++ )
printf ( "\n%s %f %d", b[i].name, b[i].price, b[i].pages ) ;

getch();
}

Sudip said: (Apr 16, 2013)  
#include<stdio.h>
#include<conio.h>

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


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


for(i=0; i<=2; i++)
printf("\n%s %f", e[i].name, e[i].sal);

getch();
}

Amit Chopra said: (May 2, 2013)  
This program is working, not even giving warnings on gcc compiler. I didn't understand whts the issue here. Please elaborate with respect to gcc?

Pathmaraj said: (Jun 23, 2013)  
I don't know how that function include libraries without calling main(). Can you please explain this concept elaborately?

Gaja said: (Sep 15, 2013)  
I don't understand about dummy variables &functions.

Valplo said: (Oct 20, 2013)  
It compiles excellent in GCC, at least. I suppose, the most modern compilers will be just as glad to compile this, So the question is a bit obsolete.

Santa said: (Dec 8, 2013)  
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: (Feb 8, 2014)  
Please help me. Is it because of & not there before e[i].

Jjb said: (May 14, 2014)  
e[i].name refers to the address so no need of &.

Pramod Mohite said: (Sep 24, 2014)  
Why the problem use the float value use in structure?

Madhu said: (Dec 9, 2014)  
Since there no addressing pointer like '&' in scanf. It generates an error.

Kunal said: (Feb 16, 2015)  
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: (Feb 16, 2015)  
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: (Apr 23, 2015)  
Floating point not linked abnormal program termination error.

Maha said: (Jul 25, 2015)  
I can't understand the answer and why it is done like that?

Can anyone explain me please.

Karthik said: (Oct 23, 2015)  
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.

Bvdkprasad said: (Nov 7, 2015)  
Useful for me in turbo C.

Ashok said: (Dec 23, 2015)  
Why don't you choose option C? Can you please explain me?

Consfused said: (Mar 17, 2016)  
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.

Subha said: (Apr 1, 2016)  
Thank you, @Badhri. Your example grasping easily.

Divya said: (Jun 7, 2017)  
While I am using float inside a structure, it is fine working in GCC compiler.

Rahul said: (Aug 6, 2017)  
#include<stdio.h>
typedef struct
{
int roll;
char name[10];
float avg;
}student;
void linkfloat() // THIS FUNCTION IS USED TO LINK THE FLOATING POINT MEMBER
{
float a = 0, *b;
b = &a;
a = *b;
}
void main(void)
{
student *s;
clrscr();
printf("\n\tEnter roll : ");
scanf("%d", &s->roll);
fflush(stdin);
printf("\n\tEnter name : ");
gets(s->name);
fflush(stdin);
printf("\n\tEnter avg : ");
scanf("%f", &s->avg);
fflush(stdin);
printf("\n\tRoll : %d", s->roll);
printf("\n\tName : %s", s->name);
printf("\n\tAvg : %f", s->avg);
getch();
}

A special function called link float ( ) should be enclosed within the scope of the program. This function will get executed automatically to link the floating point member at runtime.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.