Solved

scanf float

Posted on 2004-10-18
4
2,670 Views
Last Modified: 2008-03-17
What is the problem with this code:
float fl=0;
scanf("%.2f",&fl);

for some reason fl doesn't get the expected value. as per the spec, it seems that it is legal to use .,- before the f of float. I assume it obeys the same rules as printf. Am I wrong?

Thanks
0
Comment
Question by:doggz
  • 2
4 Comments
 
LVL 45

Expert Comment

by:Kdo
Comment Utility


You can specify a length qualifier ("%6f"), and even a complete format ("%8.3f").


What are you trying to accomplish?  I'm not sure exactly what ".2f" would represent on a scan.

Kent
0
 
LVL 5

Accepted Solution

by:
van_dy earned 30 total points
Comment Utility
Please look into the scanf manpage. it clearly specifies as follows:

 "In addition to these flags, there may be an optional maximum field width,
     expressed as a [decimal integer], between the % and the conversion"

you cannot specidy noninteger fiel widths(like .2 or 8.3) with scanf.

hope this help,
van_dy
0
 

Author Comment

by:doggz
Comment Utility
To Kent - What am I trying to achieve? same as in printf, for printing, enforce formatted input. Suppose one enters 1111.2222, and I the format is %.2f, I want the variable to get the value 1111.22. That is what I expect - I expect fl to get the number as if I was trying to print 1111.2222 with similar format. is that unreasonable?

To van_dy - take a look at page 158 at the table in K&R.
0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 20 total points
Comment Utility

Hi doggz,

You can specifcy the total field length, but not the number of digits past the decimal point (precision).  (Thanks to van_dy for the clarification -- I thought that you could specify the (field length) or (field length and precision) but apparently not.)

char *Str = "1111.2222";
float Value;

  sscanf (str, "%7f", &Value);
  printf ("%10.4f\n", Value);


The scanf() should extract "1111.22" from the string, which should be confirmed by the print:

 1111.2200


Good Luck,
Kent
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now