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 1 of 4.
Rahul said:
8 years ago
#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.
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.
(1)
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();
}
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;
}
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;
}
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?
{
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?
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.
The dummy call to a floating-point function will force the compiler to load the floating-point support and solve the original problem.
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.
main(){
struct emp{
float k;
};
struct emp e;
printf("enter multiple vallues");
scanf("%f",&e.k);
printf("%f",e.k);
}
Try it in gcc.
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.
VALPLO said:
1 decade ago
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.
Amit chopra said:
1 decade ago
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers