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;
}
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.
Sudip said:
1 decade ago
#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();
}
#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();
}
Vikas tyagi said:
1 decade ago
/* 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();
}
#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();
}
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?
Kirti said:
1 decade ago
What is emulator? What it does?
Mani Tej said:
1 decade ago
Can you explain where we can use pointers and why these are need?
Sagr said:
1 decade ago
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.
Pawan Agnihotri said:
1 decade ago
Can you tell me what is floating point format linking?
Jay said:
1 decade ago
Can you tell me what is floating point format linking?
Sri said:
1 decade ago
Is it Impossible to use scanf in structrues with array ?
ANIK SINGH said:
1 decade ago
I can't understood what you want to say so please define it briefly.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers