Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# scanf float

Posted on 2004-10-18
Medium Priority
2,738 Views
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
Question by:doggz
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 46

Expert Comment

ID: 12341473

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

van_dy earned 120 total points
ID: 12341804
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

ID: 12342504
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 46

Assisted Solution

Kent Olsen earned 80 total points
ID: 12342529

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
###### Suggested Courses
Course of the Month7 days, 23 hours left to enroll