Thread: Error: Segmentation fault (core dumped)

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    9

    Error: Segmentation fault (core dumped)

    Hi,
    I'm trying to test some basic programming with gcc and gdb, however the simple program I typed up won't compile.

    Code:
    #include <stdio.h>    // Needed for user input and screen output.
    #include <stdlib.h> // Not sure if needed, but trying to remove error: Segmentation fault (core dumped).
    
    
    int main(void)
    {
        int usersNum;    // Integer variable instantiated, to store the value of the user's number.
        
        printf("Enter a positive number: "
                
        scanf("%d", usersNum);
        printf("You entered: %d\n", usersNum);
    }
    I compile with the command $ gcc -O2 -o testFile.exe testFile.c
    and run with the command $./testFile.exe
    and receive error: Segmentation fault (core dumped).

    I then try to test with gdb to find the specific error with the command $ gdb testFile
    In gdb I use the command (gdb) run
    and receive the output:
    Starting program: /home/testFile
    [New Thread 5828.0x4b0]
    [New Thread 5828.0x1688]
    Program received signal SIGSEV, Segmentation fault.
    0x0040136e in main()

    I then compile with the optimiser -O2 and I add the flag -g to compile and enabling gdb to provide more information with command $ gcc -g -o testFile.exe testFile.c

    and run with the command $./testFile.exe
    and receive error: Bad system call

    I then try to test with gdb to find the specific error with the command $ gdb testFile
    In gdb I use the command (gdb) run
    and receive the output:
    Starting program: /home/testFile
    [New Thread 6096.0x1624]
    [New Thread 6096.0x12c0]
    [New Thread 6096.0x648]
    Program received signal SIGSEV, Bad system call.
    0x00000000 in ?? ()

    Any help please?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The problem is how you are using scanf(). scanf() needs to receive the address of variables to be read, not their value.

    And, no, #include'ing different headers will not fix your problem. <stdlib.h> is not needed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I compile with the command $ gcc -O2 -o testFile.exe testFile.c
    Compile using this command $ gcc -Wall -O2 -o testFile.exe testFile.c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 06-25-2013, 11:07 PM
  2. Segmentation Fault(core dumped) error
    By gamers18 in forum C Programming
    Replies: 5
    Last Post: 12-05-2012, 01:53 PM
  3. ERROR: segmentation fault (core dumped)
    By sunny543 in forum C Programming
    Replies: 2
    Last Post: 08-23-2012, 03:07 AM
  4. Help with 'Segmentation fault (core dumped)' error?
    By Von Fuzzball in forum C Programming
    Replies: 12
    Last Post: 05-03-2011, 02:29 PM
  5. Runtime error 'segmentation fault (core dumped)'
    By mikemccready in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:14 AM