# Segmentation Fault in example C program

• 12-02-2003
lordnothing
Segmentation Fault in example C program
okay.. i bought teh book "Teach Yourself C In 21 Days" today and have found it to be a good book to learn from.. so, here i am plodding along, typing the example programs and all of a sudden one seg faults when it comes to the 'if' statements while it's running... any of you know why such a simple program would seg fault?? here is the code so you can test it if you want.. (btw, i'm running it on a K7 (Athlon) 500 w/128mb RAM, console)
Code:

```/* Demonstrates the use of if statements. */                                                                                 #include <stdio.h>                                                                                 int x, y;                                                                                 int main&#40;&#41; &#123;         /* input the two values to be tested */                                                                                         printf&#40;"\nInput an integer value for x&#58; "&#41;;         scanf&#40;"%d", &x&#41;;         printf&#40;"\nInput an integer value for y&#58; "&#41;;         scanf&#40;"%d", y&#41;;                                                                                         /* test values and print result */                                                                                         if &#40;x==y&#41;                 printf&#40;"x is equal to y\n"&#41;;                                                                                         if &#40;x>y&#41;                 printf&#40;"x is greater than y\n"&#41;;         if &#40;x<y&#41;                 printf&#40;"x is less than y\n"&#41;;                                                                                         return 0; &#125;```
• 12-02-2003
jasonlambert
Re: Segmentation Fault in example C program
Code:

```                    printf&#40;"\nInput an integer value for x&#58; "&#41;;         scanf&#40;"%d", &x&#41;;         printf&#40;"\nInput an integer value for y&#58; "&#41;;         scanf&#40;"%d", y&#41;;```
Your missing the "&" from variable y in the 2nd scanf.

always compile your code with "-Wall" , which shows these type of errors during compile time and & "-g" which includes debugging information in the binary for use in ddd / gdb.
Code:

`gcc source.c -Wall -g -o executablefile`
Jason
• 12-02-2003
lordnothing
thanks.. don't knowmuch about the compiler options and such.. also shows what can happen when typing code at 3:30am...
• 12-04-2003
Joan Garnet
It's recommendede to
Code:

`fflush&#40;stdin&#41;;`
after scanf
• 12-04-2003
lordnothing
why's that? /me is complete C noob... hence the book and ridiculous example program that does nothing useful.
• 12-04-2003
sarumont
fflush() is a function that flushes out a stream like stdin or the file stream if you're reading a file. When scanf() takes the integer, there could be stuff hanging around in stdin. It will stay in the stream until the next function calls it. So the other scanf() would use what was left, not the new input. fflush( stdin ); fixes that. :)