-4

i am wishing to scan a float with 1 digit accuration after FP.

this is the test code:

#include <stdio.h>

void main()
{
    float f;

    scanf("%.1f",&f);
    printf("%f",f);
}

i have entered 1.234, expecting to see 1.2 but instead i was seeing this weird result : -107374176.000000

running VS2010, any ideas how to fix please?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Aviad
  • 1
  • 1
  • 2
    `void main` is neither legal C nor legal C++. You should get better learning material. – Baum mit Augen Mar 18 '17 at 20:43
  • 2
    scanf and printf don't use the same mini-language for format specifiers, and scanf can't do what you are asking. –  Mar 18 '17 at 20:44
  • http://stackoverflow.com/questions/3789997/how-do-printf-and-scanf-handle-floating-point-precision-formats – Jerfov2 Mar 18 '17 at 20:46
  • Floating point variables in C know nothing about "digit accuracy". Always work to the best accuracy available, and reduce that for output. It is bad enough that floating point variables cannot store an exact representation of every real number. – Weather Vane Mar 18 '17 at 20:47
  • @BaummitAugen C specifies certain function signatures for `main()`, `void main()` is not a specified one yet others are allowed per "... in some other implementation-defined manner." C11 5.1.2.2.1 1 So for OP [VS2010](https://msdn.microsoft.com/en-us/library/6wd819wh(v=vs.100).aspx) **is legal** and confirms to C with implementation-defined code. You should get better learning material – chux - Reinstate Monica Mar 18 '17 at 22:19
  • "wishing to scan a float with 1 digit accuration (precision) after FP" --> OK, what should code do with characters the follow like `"34"`? Declare an error, quietly ignore them, leave them for the next input function? There is an answer for your coding coding, but it is presently too vague. – chux - Reinstate Monica Mar 18 '17 at 22:28
  • @chux Fair enough, I deserved that one. Still, if their learning material needlessly teaches implementation specific stuff like that to beginners, I suggest to use something else. – Baum mit Augen Mar 18 '17 at 22:29
  • @BaummitAugen I [2nd](https://en.wikipedia.org/wiki/Second_(parliamentary_procedure)) your [_suggestion_](http://stackoverflow.com/questions/42879595/in-c-asking-to-scanf-1-digit-after-point-and-getting-gybrish?noredirect=1#comment72864704_42879595). – chux - Reinstate Monica Mar 18 '17 at 22:35

3 Answers3

1

It prints random numbers because the call to scanf failed and you are printing an uninitialized variable.

The floating point precision specification only applies in calls to printf, not to scanf. The format specifier you gave is simply invalid.

You would have known the call failed, had you checked the return value of scanf, which is equal to the number of format specifiers that were successfully converted.

If you want a specific accuracy for the number, you'd need to read it in full and then preform the truncation/rounding operation.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
1

You can't do that, you can make scanf() read a specific number of characters if you want like

float value;
scanf("%4f", &value);

and suppose the input is

43.23
it will read

43.2

but you can't specify precision.

what you can do is

float value;
if (scanf("%f", &value) == 1)
    printf("%.2f\n", value);

after all, the precision is limited by the binary representation, so making it have only two decimal places is pointless since in arithmetic operations it might be rounded.

Tanuj Yadav
  • 1,259
  • 13
  • 21
0
scanf("%.1f",&f);

Your format specifier in the scanf() function is incorrect; you cannot specify the number of decimal places you want in as input for scanf(), you can only do that when you want to output the floating point number using printf().

You need to accept the input in full with scanf():

scanf("%f",&f);

And then print out the number you want to the desired number of decimal places:

printf("%.1f", f);

This will print out 1.234 as 1.2.


Also, main() needs to have a return type of int by default; void main() is not legal in C or C++. Even if you do not want to return anything from main(), define it as:

int main(void)
BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31