2016-03-20 14 views
0

Dies ist das C-ProgrammC-Programm - Doppel null zurück

#include <stdio.h> 
#include <stdlib.h> 

int main (void){ 

    double voltage, resistance; 
    printf("Enter your Voltage: "); 
    scanf("%f", &voltage); 
    printf("Enter your Resistance: "); 
    scanf("%f", &resistance); 
    double amps = voltage/resistance; 
    printf("With voltage %f and Ohm %fohms, amps is equal to %f \n", voltage, resistance, amps); 
    return 0; 
} 

Warum sind Spannungs- und Widerstandsgrößen Rückkehr Wert von 0,00 ist?

+0

Hey danke, das hat funktioniert, kannst du bitte erklären, was das Hinzufügen des 'l' tut? – Vbobo

+0

Vielleicht möchten Sie RTFM auf 'scanf()': http://www.iso-9899.info/n1570.html#6.2.5p28 – alk

+0

Ihr Compiler sollte vor der Diskrepanz zwischen Format-Spezifizierer und der Art des Arguments gewarnt haben bestanden. Wenn nicht, erhöhen Sie die Warnstufe. Warnungen ernst nehmen. – alk

Antwort

2

Sie sollten %lf in Ihrem scanf verwenden.

Das funktionierte für mich schön:

#include <stdio.h> 
#include <stdlib.h> 

int main (void){ 

double voltage, resistance; 
printf("Enter your Voltage: "); 
scanf("%lf", &voltage); 
printf("Enter your Resistance: "); 
scanf("%lf", &resistance); 
double amps = voltage/resistance; 
printf("With voltage %f and Ohm %fohms, amps is equal to %f \n", voltage, resistance, amps); 
return 0; 
} 
3

Für scanf (and family) das Format "%f" für float ist, ein double Sie eine "lange" float erhalten müssen, die das Format-Präfix l wie in "%lf" haben.