Write a function sum such that the call sum(a,b,c) will store the sum of a and b in c

Source Code

Brief explanation is provided after the source code.

#include <stdio.h>

void sum(int, int, int);

int main(int argc, char ** argv) {

    int a, b, c = 0;
    printf("Enter a and b:\n");
    scanf("%d%d", &a, &b);
    sum(a, b, c);
    return 0;

void sum(int a, int b, int c) {

    c = a + b;
    printf("c: %d\n", c);

When you compile and execute the above program it produces the following result on Linux:

Enter a and b:
c: 11

Brief Explanation

  • The program starts by initializing the variables a, b and c to zero.
  • Next, the user is requested to enter the values of a and b.
  • Using the function scanf, the numbers are read and assigned to the integer variables a and b. scanf reads characters from the standard input, interprets them according to the specification in format, and stores the results through the remaining arguments. scanf stops when it exhausts its format string, or when some input fails to match the control specification.
  • The function call sum(a,b,c) computes the sum of a and b and store the result in c.
  • Using the printf function, the value of c is printed to the standard output.
  • The return value of zero in the main function implies normal termination; non-zero values signal unusual or erroneous termination conditions.

Add comment

Security code