Author Topic: simple fahrenheit to celsius segmentation fault? wtf?  (Read 1655 times)

0 Members and 1 Guest are viewing this topic.

Offline Polyphony

  • VIP
  • Knight
  • *
  • Posts: 178
  • Cookies: 23
    • View Profile
simple fahrenheit to celsius segmentation fault? wtf?
« 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.
Code: [Select]
<Spacecow_> for that matter I have trouble believing bitches are made out of ribs
<Gundilido> we are the revolutionary vanguard fighting for the peoples right to display sombrero dawning poultry
<Spacecow> did they see your doodle?
<~phage> Maybe
<+Unresolved> its just not creative enough for me
<+Unresolved> my imagination is to big to something so simple

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: simple fahrenheit to celsius segmentation fault? wtf?
« Reply #1 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;
}

Offline Polyphony

  • VIP
  • Knight
  • *
  • Posts: 178
  • Cookies: 23
    • View Profile
Re: simple fahrenheit to celsius segmentation fault? wtf?
« Reply #2 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
Code: [Select]
<Spacecow_> for that matter I have trouble believing bitches are made out of ribs
<Gundilido> we are the revolutionary vanguard fighting for the peoples right to display sombrero dawning poultry
<Spacecow> did they see your doodle?
<~phage> Maybe
<+Unresolved> its just not creative enough for me
<+Unresolved> my imagination is to big to something so simple