C/C - floating point
Expert: Narendra - 6/27/2005
QuestionI have 2 problems in C regarding floating point, please help :
(I am using turbo C++ ver-3.0 and saving as .C extension.This compiler is fully compatible with C89 standards.)
Q1
Output of the following code- C.
main()
{
float a=0.7;
if(a<0.7)
printf("C");
else
printf("C++);
}
But if we change the if condition from a<0.7 to a<0.7f we get output- C++.
Please tell the reason for both the outputs because i think it should print C++ in both the cases.
Q2
The following code gives runtime error- Floating point formats not linked. When does this error occur in a program.
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);
}
AnswerThe reason for this is that, the real numbers are not represented in the way you see it.
So, 0.7 could have been 0.69999999 or 0.70000000000000001.
> The following code gives runtime error
Your code must be giving compilation error, not run time error.
The reason for the error is
> scanf("%s %f",e[i]name, &e[i].sal);
Correct it as:
scanf("%s %f",e[i].name, &e[i].sal);
So, here is the corrected code:
#include <stdio.h>
main()
{
struct emp
{
char name[20];
float sal;
};
struct emp e[10];
int i;
for(i=0;i<=2;i++)
scanf("%s %f",e[i].name, &e[i].sal);
}