Thread: Segmentation fault: 11

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    7

    Segmentation fault: 11

    I have to make a program that Reads two numbers and check if the first one is a multiple of the second.
    I think the whole program I wrote is wrong , but when I compile it I get "Segmentation fault: 11"

    Here's what I wrote:
    Code:
    
    int main(void)
    {
        int number1;
        int number2;
        int number1x;
        int number2x;
    
        printf("input the 1st number ");
        scanf("%d", number1);
        printf("\ninput the 2nd number: ");
        scanf("%d", number1);
        printf("\n");
    
        number1x = number1 * 2;
        number2x = number2 * 2;
    
        if(number1 == number1x)
        {
            printf("is a multiple");
        }
        else
        {
            printf("non  multiplo");
        }
    
        if(numero2 == numero2x)
        {
            printf("is a multiple");
        }
        else
        {
            printf("is not a multiple");
        }
    
        printf("program exited");
    
    
    
    
    }


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,341
    You're missing a header inclusion, i.e., you should #include <stdio.h>

    Your compiler should be warning you about your scanf calls: you need to pass a pointer to the int object, e.g., this:
    Code:
    scanf("%d", number1);
    should have been:
    Code:
    scanf("%d", &number1);
    or better yet, check the return value of scanf (but you need to #include <stdlib.h> for EXIT_FAILURE):
    Code:
    if (scanf("%d", &number1) != 1)
    {
        fprintf(stderr, "Error: could not read the first number.\n");
        return EXIT_FAILURE;
    }
    This failure to pass a pointer to scanf is likely the cause of the segmentation fault.

    You don't need number1x and number2x: recall what "multiple" means; it doesn't just mean "twice".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Segmentation fault
    By shashinitjsr in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2012, 01:37 PM
  3. Segmentation Fault
    By jacobj86 in forum C Programming
    Replies: 3
    Last Post: 03-15-2012, 08:00 PM
  4. Getting a segmentation fault.
    By mgracecar in forum C Programming
    Replies: 13
    Last Post: 02-22-2012, 09:23 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread