How to read in a negative double with scanf() in C

Posted by rize on Stack Overflow See other posts from Stack Overflow or by rize
Published on 2010-05-30T19:02:09Z Indexed on 2010/05/30 19:22 UTC
Read the original article Hit count: 261

Filed under:
|
|
|

I'm learning basics of C and writing a simple first order equation solver. I want the input to be exactly ax+b=c or ax-b=c, where a, b, c are double type. I'm employing scanf() to read in user input and to check if it's of the correct form. However, if I enter a negative double, -4.6 say, as the "a" in the equation, scanf() won't read the a,b,c correctly. I'm using %lf inside scanf(). How do I read a negative double, then? Many thanks.

My code:

if (scanf("%lfx+%lf=%lf", &a, &b, &c))
    more code

If I use as the input "-6.2x+3.4=-5.9", the value 3.4 will be assinged to variable a, while b and c remain as they were and "more code" is run.

© Stack Overflow or respective owner

Related posts about c

    Related posts about double