Problem with scanf on solaris 10
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Problem with scanf on solaris 10




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Problem with scanf on solaris 10  
ramanchalotra@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-06 12:57 PM

Deat All

I have written a piece of code as


#include<stdio.h>


main()
{
float f;
printf("\n Enter a float value ");
scanf("%f",&f);
printf("\n Value = %f\n ",f);




}



when i ran this code the output looks  as



Enter a float value  1234.678
Value = 1234.677979


Where i am making mistake ..

Please help

Regards,
Raman






[ Post a follow-up to this message ]



    Re: Problem with scanf on solaris 10  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-06 12:57 PM

ramanchalotra@gmail.com writes:

> Deat All
> I have written a piece of code as
> #include<stdio.h> main()
> {
>  float f;
>  printf("\n Enter a float value ");
>  scanf("%f",&f);
>  printf("\n Value = %f\n ",f);
> }
> when i ran this code the output looks  as
> Enter a float value  1234.678
>  Value = 1234.677979

Which is perfectly correct.


> Where i am making mistake ..

The mistake is in not reading:
What Every Computer Scientist Should Know About Floating-Point Arithmetic
http://portal.acm.org/citation.cfm?id=103163




If you expect a number with 7 significant decimal digits (ie. a C
float), you should print only 7 significant decimal digits:

-->  printf("\n Value = %.6e\n ",f);

[pjb@thalassa tmp]$ ./f

Enter a float value 1234.678

Value = 1.234678e+03


--
__Pascal Bourguignon__                     http://www.informatimago.com/

"You question the worthiness of my code? I should kill you where you
stand!"





[ Post a follow-up to this message ]



    Re: Problem with scanf on solaris 10  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-06 12:57 PM

ramanchalotra@gmail.com wrote:

>  float f;
>  printf("\n Enter a float value ");
>  scanf("%f",&f);
>  printf("\n Value = %f\n ",f);

> Enter a float value  1234.678
>  Value = 1234.677979

> Where i am making mistake ..

Hi Raman,

The only mistake I can see, is that you expect the float
value be "exactly" like you entered it.

Due to the nature of floating-point representation there
is no "exact" representation for this number. What is
stored is the nearest float-number to the one you wanted.



... Rainer





[ Post a follow-up to this message ]



    Re: Problem with scanf on solaris 10  
Andrei Voropaev


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-06 12:57 PM

On 2006-02-15, ramanchalotra@gmail.com <ramanchalotra@gmail.com> wrote:
> I have written a piece of code as
[...]
>  printf("\n Enter a float value ");
>  scanf("%f",&f);
>  printf("\n Value = %f\n ",f);
[...]
> when i ran this code the output looks  as
> Enter a float value  1234.678
>  Value = 1234.677979
>
> Where i am making mistake ..

Nowhere. Everything is correct. Computer can't handle floats absolutely
precise like integers. Read http://en.wikipedia.org/wiki/Floating_point



--
Minds, like parachutes, function best when open





[ Post a follow-up to this message ]



    Re: Problem with scanf on solaris 10  
Giorgos Keramidas


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-15-06 12:57 PM

On 15 Feb 2006 04:00:56 -0800, ramanchalotra@gmail.com wrote:
> Deat All
>
> I have written a piece of code as
>
> #include<stdio.h>
>
> main()
> {
>  float f;
>  printf("\n Enter a float value ");
>  scanf("%f",&f);
>  printf("\n Value = %f\n ",f);
> }
>
> when i ran this code the output looks  as
>
> Enter a float value  1234.678
>  Value = 1234.677979
>
> Where i am making mistake ..

There are various `problems' with this program:

- The main function is incorrect.  Sun Studio 10 reports:

"foo.c", line 5: warning: old-style declaration or incorrect type for: main

A similar message is displayed by GCC:

foo.c:5: warning: return type defaults to `int'

- You are not returning any status at all from main(), even though its
lack of a proper function declaration means it defaults to 'returning
an int'.

- Output to stdout may be line-buffered.  This means that your prompt,
which doesn't end with a newline may never appear until a newline has
reached standard output.  Your program will then happily block,
waiting for input in scanf(), but the user won't know anything about
the reason the program appears to do nothing at all.  Not good.

You should definitely use something like:

printf("Enter a float value: ");
fflush(stdout);

- It's probably bad style to print a newline and then a space, as in:

printf("\n Foo");

This ends up showing as a very awkward space at the first column of my
terminal:

|keramida@beatrix:/home/keramida$ ./a.out
|
| Enter a float value: 123
|
| Value = 123.000000
|keramida@beatrix:/home/keramida$

Similarly, printing a newline right after scanf() has returned from a
line-buffered input call, results in a 'double newline', shown above
as an empty line between the two lines displayed by your printf()
calls.

- Most importantly, you are using 'float', which has limited precision.
It's not strange that you get a limited precision.  Try this instead:

1  #include <stdio.h>
2  #include <stdlib.h>
3
4  int
5  main(void)
6  {
7          double f;
8
9          printf("Enter a float value: ");
10          fflush(stdout);
11
12          scanf("%lf", &f);
13
14          printf("Value = %lf\n", f);
15          return EXIT_SUCCESS;
16  }

This should work fine:

keramida@beatrix:/home/keramida$ cc -Xc foo.c -lm
keramida@beatrix:/home/keramida$ ./a.out
Enter a float value: 1234.678
Value = 1234.678000
keramida@beatrix:/home/keramida$






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 05:49 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register