C Programming: Sum of Three Integers

#include <stdio.h>

int main() {
    int first, second, third;
    int total;
    
    printf("Enter three integers separated by spaces:\n");
    scanf("%d %d %d", &first, &second, &third);
    
    total = first + second + third;
    printf("Sum: %d\n", total);
    
    return 0;
}

The program begins by declaring four integer variables: first, second, third, and total. It then prompts the user to input three inteegrs using scanf with format specifiers %d %d %d to readd values into the respective variables.

After reading the inputs, the program calculates their sum and stores the result in the total variable. Finally, it outputs the computed sum using printf with the format specifier %d.

For comparison, here's the alternative version from the textbook:

#include <stdio.h>

int main() {
    int first, second, third;
    int total;
    
    first = 123;
    second = 456;
    third = 789;
    
    total = first + second + third;
    printf("Sum: %d\n", total);
    
    return 0;
}

This version hardcodes the values directly in the source code instead of accepting user input.

Tags: c programming integer arithmetic user input scanf printf

Posted on Thu, 07 May 2026 09:39:01 +0000 by ridgedale