简体   繁体   中英

C - Segmentation Fault (core dumped)

I am trying to write a simple C program that takes the input of a number and returns the sum of the number's digits as well as the reverse of the number. The program is currently incomplete because I keep encountering Segmentation Fault errors when testing it.

Here is the code:

#include <stdio.h>
#include <string.h>

int sumdigits(int);

int main() {
    int num;                // The number to be read
    int reverse;            // The reverse of the input
    int sum;                // The sum of the digits


    printf("Enter a number: ");
    scanf("%d", &num);
    sum = sumdigits(num);
    printf("Sum of digits: %s", sum);
}

int sumdigits(int number) {
    int sum = 0;
    int temp = number;
    while (temp != 0) {
        sum += (temp % 10);
        temp /= 10;
    }
    return sum;
}

By using some print statements, I've discovered that the error most likely occurs somewhere in these lines:

printf("Enter a number: ");
scanf("%d", &num);

I am able to enter a number, but immediately after that I get a message saying Segmentation Fault (core dumped) and the program exits. Running the program with sudo privileges just provides Segmentation Fault, without the "core dumped" part of the message.

Debug info provided by instructions followed here seems to indicate that the printf statement is what is causing the error, as this is the output:

(gdb) run
Starting program: /home/sschmalz/Documents/Classes/CIS308/proj1/debug 
Enter a number: 123

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a62fb4 in vfprintf () from /lib64/libc.so.6

However, I don't know what to do with this information, so I can't resolve the error.

I have asked my professor for help with this same error in lab, and he was unable to find a solution. This has happened in two different labs now, plus with this project, which makes me think that it may be something with my computer's setup rather than with the code itself.

I am writing the program using vim and compiling using gcc. In most cases I am not renaming the output of the compiler, so I am running it using "./a.out" I am using Fedora 24 to write and run these programs. If any other information about my system is needed, please let me know.

You have used incorrect format specifier in the printf() functions, modify it to:

printf("Sum of digits: %d", sum);

and it will work correctly ( %d stands for decimal (numbers) and %s stands for strings).

Change

printf("Sum of digits: %s", sum);

to

printf("Sum of digits: %d", sum);

%s is for strings not for integers ( %d is for signed integers).

See http://www.cplusplus.com/reference/cstdio/printf/

I ran your code and it works but you have to change the printf("Sum of digits: %s", sum); to either printf("Sum of digits: %i", sum); or printf("Sum of digits: %d", sum);

There is no difference in using %d or %i in printf() because:

  1. The conversion specifiers and their meanings are: d,i - the int argument is converted to signed decimal in the style [−]dddd. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is no characters. Source: C99 standards http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

However, there is a difference when using %d or %i in scanf() because the former is stored as a decimal integer and the latter as an integer. Source: https://cs50.harvard.edu/resources/cppreference.com/stdio/scanf.html

The code I ran that works:

#include <stdio.h>
#include <string.h>

int sumdigits(int);

int main() {
    int num;                // The number to be read
    int reverse;            // The reverse of the input
    int sum;                // The sum of the digits


    printf("Enter a number: ");
    scanf("%i", &num);
    sum = sumdigits(num);
    printf("Sum of digits: %i", sum);
}

int sumdigits(int number) {
    int sum = 0;
    int temp = number;
    while (temp != 0) {
        sum += (temp % 10);
        temp /= 10;
    }
    return sum;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM