Probleme mit scanf

Post Reply
Message
Author
Tim+Happel

Probleme mit scanf

#1 Post by Tim+Happel »

Hallo!
Ich versuche einfach nur einen double-Wert mit scanf einzulesen... Was für einen Fehler mache ich?

#include <stdio.h>

int main()
{
double test;
printf("Wert für test eingeben: "); //23.2
scanf("%f", &test);
printf("test: %f
", test); //-1.996983
}

Vielen Dank im Voraus,

Tim

W. Arnungen

Re: Probleme mit scanf

#2 Post by W. Arnungen »

gcc -Wall .....

Tom

Re: Probleme mit scanf

#3 Post by Tom »

wenn ich mich nicht irre (habe momentan kein Manual zur Hand), liefert scanf mit den Flags %f %e %E und %g %G einen Zeiger auf float zurück, und nicht auf double. Du könntest aber mit scanf einen character %c einlesen und dann mit atof in einen double umwandeln.
Evtl. hat noch jemand eine bessere Lösung?

Tom

User avatar
hjb
Pro-Linux
Posts: 3264
Joined: 15. Aug 1999 16:59
Location: Bruchsal
Contact:

Re: Probleme mit scanf

#4 Post by hjb »

Hi!

in scanf braucht %f einen Pointer auf float, in printf erwartet %f ein double...

Benutze %lf bei scanf.

Gruß,
hjb
Pro-Linux - warum durch Fenster steigen, wenn es eine Tür gibt?

Tim Happel

Re: Probleme mit scanf

#5 Post by Tim Happel »

mensch vielen Dank! Hat mir geholfen <img src="http://www.pl-forum.de/UltraBoard/Images/Happy.gif" border="0" align="middle">

Post Reply