# Thread: Segmentation fault in C

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 &lt;stdio.h&gt;
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.

2. scanf("%3d", celsius);
where is the & it should written as
scanf("%d", &celsius);

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

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. 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. it's always the smallest thing

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 &lt; 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. 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

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

10. And the security aspect of this thread is?

Page 1 of 3 123 Last

#### Posting Permissions

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