EvilZone

Programming and Scripting => C - C++ => Topic started by: Polyphony on October 02, 2012, 01:47:47 AM

Title: simple fahrenheit to celsius segmentation fault? wtf?
Post by: Polyphony on October 02, 2012, 01:47:47 AM
Code: [Select]
#include <stdio.h>
int main()
{
    float f, answer;
    answer= 5.0 / 9.0 * (f-32.0);   
    printf("fahrenheit =");
    scanf("%f", f);
    printf("\n");
    printf("%.1f to celsius is %.1f\n", f, answer);
   
    return 0;
}

is it because I'm using scanf() wrong? the compiler (GCC) gives me 2 warnings...
fahrenheit_to_celsius.c:7:2: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
fahrenheit_to_celsius.c:5:24: warning: ‘f’ is used uninitialized in this function [-Wuninitialized]

I don't really get the 1st error as I know at the top i initialized the variables as floats.
Title: Re: simple fahrenheit to celsius segmentation fault? wtf?
Post by: ca0s on October 02, 2012, 12:48:53 PM
Code: [Select]
-- scanf("%f", f);
++ scanf("%f", &f);
And you are performing your operations before getting the input from the user. Should be
Code: [Select]
#include <stdio.h>
int main()
{
    float f, answer;
    printf("fahrenheit =");
    scanf("%f", f);
    answer= 5.0 / 9.0 * (f-32.0);   
    printf("\n");
    printf("%.1f to celsius is %.1f\n", f, answer);
   
    return 0;
}
Title: Re: simple fahrenheit to celsius segmentation fault? wtf?
Post by: Polyphony on October 03, 2012, 01:41:01 AM
gotcha, simple noob mistake : / I guess I wasn't thinking clearly when I was programming this ;) thanks for the solution