|
-
July 11th, 2004, 12:34 PM
#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>
main()
{
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.
-
July 11th, 2004, 02:15 PM
#2
Junior Member
scanf("%3d", celsius);
where is the & it should written as
scanf("%d", &celsius);
-
July 11th, 2004, 03:14 PM
#3
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?
i2c
-
July 11th, 2004, 07:03 PM
#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
-
July 11th, 2004, 08:53 PM
#5
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
#6
Banned
it's always the smallest thing
-
July 12th, 2004, 12:30 AM
#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
-
July 12th, 2004, 02:33 PM
#8
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.
-
July 12th, 2004, 04:27 PM
#9
I once (quite recently), spent about an hour debugging this line:
Code:
if(i=true)
blablabla;
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.
Cheers,
cgkanchi
-
July 12th, 2004, 04:48 PM
#10
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|