Segmentation fault in C
Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Segmentation fault in C

  1. #1

    Segmentation fault in C

    hi all,

    i'm learning C at the moment, and one of the programs given as example converts fahrenheit to celsius.
    But since i like to reverse the program, to convert celsius to fahrenheit, i've rewritten it to learn from it.

    only problem is, when i run it and enter a value for celsius, i get an segmentation fault.

    what did i do wrong? here is the source:
    #include <stdio.h>
    int celsius;
    float fahr;
    printf("This is very a simple program which allows\n");
    printf("you to convert a celsius value to fahrenheit.\n");
    printf("\nType the value of celsius you want converted:\n");
    scanf("%3d", celsius);
    fahr = celsius / (5.0/9.0) + 32;
    printf("%3d degrees celsius is %3.2f degrees fahrenheit.", celsius, fahr);
    return 0;
    i've tried changing the var fahr to a double, and even an int, but the same problem still persists..

    and when i do the calculation with a scientific calculator the result is correct.

  2. #2
    Junior Member
    Join Date
    Jul 2004
    scanf("%3d", celsius);
    where is the & it should written as
    scanf("%d", &celsius);

  3. #3
    Senior Member
    Join Date
    Jul 2003
    Yea power_user's correct, but you can use the 3d I think, becuase doesnt that just specify the number of decimal places you want it to?


  4. #4
    ooohhh sh*t, that i've missed that, and yes, the 3d just specifies the maximum input, 3 characters...
    so a max of 999 integer...

    but i feel very stupid i've forgot the & sign

    thanks a lot for the help

  5. #5
    Senior Member
    Join Date
    Feb 2004
    That was always the one that got me when I started coding in C. The damn "&' character when using scanf!! I got used to it after a while though.

  6. #6
    Join Date
    Nov 2003
    it's always the smallest thing

  7. #7
    yes indeed, last time i helped someone else out with his code, he had the following string in his code:

    for (i = 0; i < 10; i++);

    and he wonderd why it didn't work.. he typed a ';' to much, but i saw it right away... this means that just a thing so little, although you know how it works, you just don't see it, and someone else spots it in a second....

    strange thing, how the human mind works

  8. #8
    Just Another Geek
    Join Date
    Jul 2002
    Rotterdam, Netherlands
    I've been programming for some years now but pointers, references, badly placed semi-colons and misplaced quotes are still the source of most of my bugs
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  9. #9
    Antionline Herpetologist
    Join Date
    Aug 2001
    I once (quite recently), spent about an hour debugging this line:

    If you can't see what's wrong, it should be == instead of =. Using = causes i to be assigned the value true and the condition is always true. I've been programming for about 10 years now, and I still made this boo boo.

    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog:

  10. #10
    rebmeM roineS enilnOitnA steve.milner's Avatar
    Join Date
    Jul 2003
    And the security aspect of this thread is?
    IT, e-commerce, Retail, Programme & Project Management, EPoS, Supply Chain and Logistic Services. Yorkshire.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts