Search code examples
cscanfconversion-specifier

Why is the sum of two Integers in this case not working?


I'm new to C Programming and I am stuck on a simple problem. Here is the following code...

#include <stdio.h>

/* Write a C Program that accepts two integers from the 
    user and calculate the sum of the two intergers. Go to the editor!
*/

int main() 
{
    
    int firstInteger, secondInteger;
    int sum;
    
    printf("Input two Integers: \n");
    scanf("%d%d", &firstInteger, &secondInteger);
    
    sum = firstInteger + secondInteger;
     printf("%d", sum);

    return 0;
}

After I run my code on the GCC compiler I don't get what I expect!

C:\XXXX\XXXX\XXXX\XXXX\XXXX>gcc 2sumOfTwoIntegers.c

C:\XXXX\XXXX\XXXX\XXXX\XXXX>a
Input two Integers:
25, 38
25

Why don't I get the sum of my inputted Integers?


Solution

  • scanf() don't skip , in the input by default and stop there because it cannot be interpreted as integer.

    Add , to the format like this to have scanf() read and drop ,.

        scanf("%d,%d", &firstInteger, &secondInteger);