C program for adding two numbers

In the previous article we learnt how to display Hello world in C programming but it was not that useful program so today in this article we will write a C program for adding two numbers.

C program for adding two numbers is given below you can just copy it and paste it in codeblock and run it.


Output  of program 

Explanation of code

At first  all the lines which starts with /* is comment and these lines are ignored by compiler we will learn more about comments later but for now just know that comments are for documentation and for developer and it is not compiled or run and ignored by compiler.

int a , b , sum; 
This line declares three variable a , b and sum of type integer . variable are storing locations of data in memory in c and these variables store integers  i.e negative , positive or zero.

printf("Enter first number:");
This line display a message to user on screen to input a number.

This line gives input field on screen and when user input a number that number get stored to address of "a" variable ( & is read as address of ).

Next lines are same for taking second number and storing it in variable "b"

sum = a+b;
This line initialize value "a+b" in sum, i.e add a and b and store it in variable sum

printf("sum  of %d and %d is %d",a,b,sum);
This line display sum of two numbers  to user in formatted way on screen. %d  is format specifier for int data type . we will learn more about format specifier later. For now just know that %d is for int type , %c is for char datatype , %s for character array or string.

Contact Form


Email *

Message *