C – Program to Add Numbers With Explanation

Program to Add Numbers

This is the C program for adding two numbers, The program will ask to user to enter two numbers and that will store the result of the two number is another variable and that will give the output. 

C Code

#include<stdio.h>
int main(void){
    int a, b, c; 
    printf("Enter two numbers:");
    scanf("%d%d",&a,&b);
c=a+b;
    printf("n sum of Two Numbers is %d",c);
    return 0; 
}

C Output

Enter two numbers:
1
2
sum of Two Numbers is 3

Let us discuss about the program, 

  • int a, b,c;  This statement is a variable declaration statement a,b and c are the three variables in the program. int is a keyword (which is reserved word) which tells the computer that a, b, and c are of type integer. In C each and every variable or constant must be declared before its use.  
  • scanf(); Like printf(), scanf() is also a library function but it accepts input from the use. This function waits until the user enters the value. With scanf() function, we must write ampersand (&) sign before any variable while applying arguments. Ampersand (&) is an address operator that gives the address of the variable written after it.   A suitable conversion-specification must be included within double quotes as arguments with scanf() function. In the above example, %d is conversion specification for the integer data type. 
  • c=a+b This statement causes a variable a and b to be added and assigned to c. Here + is an addition operator and = is an assignment operator. Assignment causes the value to be stored in a variable which is at the left of the assignment operator. Hence, the sum of a and b is stored in variable c.
  • printf(“nSum of two numbers is %d”, c);printf() is a library function to display just the string or the value of the variable within the string. The first argument in scanf() or printf() is called the control string. The %d in the control string is called conversion-specification, which displays the variable in the proper location and with proper data-type. The d in %d is called conversion character for the integer data type. If the user supplies two values 10 and 20 then this line displays the output as the sum of two numbers is 30.