Thread: Segmentation fault (core dumped)

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Segmentation fault (core dumped)

    I am new to programming and am following c programming for the absolute beginner. On two different programs I get this message when I run the program. Segmentation fault. What is Segmentation fault? What causes it? How do i fix it in the program listed. Thanks for the help.

    Code:
    #include <stdio.h>
    main()
    {
    
    int x, iNumQuestions, iResponse, iRndNum1, iRndNum2;
    
    srand(time());
    printf("\nEnter number of questions to ask: ");
    
    scanf("%d", &iNumQuestions);
    for ( x = 0; x < iNumQuestions; x++ ) {
    
    iRndNum1 = rand() % 10 + 1;
    iRndNum2 = rand() % 10 + 1;
    
    printf("\nWhat is %d x %d: ", iRndNum1, iRndNum2);
    scanf("%d", &iResponse);
    
    if ( iResponse == iRndNum1 * iRndNum2 ) 
    printf("\nCorrect!\n");
    
    else
    printf("\nThe correct answer was %d \n", iRndNum1 * iRndNum2);
    
    } //end for loop

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What is Segmentation fault? What causes it? How do i fix it in the program listed.
    A segmentation fault is usually caused by a program trying to access memory that does not belong to the program.

    Your problem may be caused by these error messages that I received when I compiled your program. It looks like you are missing a few include files. And the function main() should be defined as returning an int, and you should return an int from this function.

    main.c|2|error: return type defaults to ‘int’|
    main.c||In function ‘main’:|
    main.c|7|error: implicit declaration of function ‘srand’|
    main.c|7|error: implicit declaration of function ‘time’|
    main.c|13|error: implicit declaration of function ‘rand’|
    ||=== Build finished: 4 errors, 0 warnings ===|
    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    look up the definition of the 'time' function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault (core dumped)
    By sameer2904 in forum C Programming
    Replies: 3
    Last Post: 01-09-2012, 07:37 AM
  2. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM
  3. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM