Find the answer to your Linux question:
Results 1 to 6 of 6
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, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078

    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;
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  2. #2
    Linux Guru
    Join Date
    Apr 2003
    Location
    London, UK
    Posts
    3,284

    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

  3. #3
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    thanks.. don't knowmuch about the compiler options and such.. also shows what can happen when typing code at 3:30am...
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jul 2003
    Location
    BCN
    Posts
    17
    It's recommendede to
    Code:
    fflush&#40;stdin&#41;;
    after scanf

  6. #5
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    why's that? /me is complete C noob... hence the book and ridiculous example program that does nothing useful.
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  7. #6
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    Location
    /dev/urandom
    Posts
    3,682
    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.
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

Posting Permissions

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