0

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.

Community
  • 1
  • 1
  • 1
    Undefined behavior for passing an `int` to `printf()` with a `%s` conversion. – EOF Sep 17 '16 at 13:03
  • My compiler on my computer didn't catch the string formatting error for some reason. I uploaded the file to my university's Linux server and compiled it on there, which gave me the exception and the fix. Thank you all for your help, I feel like such an idiot for such a small problem. – Sam Schmalzried Sep 17 '16 at 13:15

3 Answers3

0

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).

syntagma
  • 23,346
  • 16
  • 78
  • 134
0

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/

MrTux
  • 32,350
  • 30
  • 109
  • 146
  • Thank you for pointing this out. For some reason the compiler on my computer didn't catch this, but when I uploaded it to my university's Linux server and compiled, it threw an exception. – Sam Schmalzried Sep 17 '16 at 13:13
  • @SamSchmalzried That's not something that will be caught generally, since all any compiler is required to know or care about is that `printf()` takes an arbitrary string and a set of variadic (and therefore necessarily arbitrary) arguments, both of which you've provided. Checking whether or not the arguments you provided make sense in combination would require a lot of acrobatics - and possibly runtime instrumentation for the variadic arguments, idk - which most compilers won't waste their time doing, unless you explicitly tell them to. Otherwise, they just assume your arguments do make sense. – underscore_d Sep 17 '16 at 15:28
0

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;
}