You are here:

C/C - floating point

Advertisement


Question
I 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);
}  

Answer
The 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);
}

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.