C - Program to Add Numbers With Explanation

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.

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 variables or constants 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 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 above example, %d is conversion specification for 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 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 proper location and with proper data-type. The d in %d is called conversion character for integer data type. If user supplies two values 10 and 20 then this line displays output as, sum of two numbers is 30. 

You May Also Like...

Socialize with Us