July 11th, 2004, 12:34 PM
Segmentation fault in C
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:
i've tried changing the var fahr to a double, and even an int, but the same problem still persists..
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");
fahr = celsius / (5.0/9.0) + 32;
printf("%3d degrees celsius is %3.2f degrees fahrenheit.", celsius, fahr);
and when i do the calculation with a scientific calculator the result is correct.
July 11th, 2004, 02:15 PM
where is the & it should written as
July 11th, 2004, 03:14 PM
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?
July 11th, 2004, 07:03 PM
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
July 11th, 2004, 08:53 PM
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.
July 11th, 2004, 09:22 PM
it's always the smallest thing
July 12th, 2004, 12:30 AM
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
July 12th, 2004, 02:33 PM
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
Experience is something you don't get until just after you need it.
July 12th, 2004, 04:27 PM
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.
July 12th, 2004, 04:48 PM
And the security aspect of this thread is?
IT, e-commerce, Retail, Programme & Project Management, EPoS, Supply Chain and Logistic Services. Yorkshire. http://www.bigi.uk.com