C Program to Add Two Integers
In this article, you will be instructed on how to write a C program that adds two integers. Adding two integers is an extremely simple C program where the user is prompted to enter two integers, after which the program reads the inputs, saves them in two different variables, and then shows the overall sum of the two integers.
Example:
Input: x= 10, y= 23
Output: z = (x + y) = (10 + 23) = 33
Input: x= 45, y= 13
Output: z = (x + y) = (45 + 13) = 58
When writing a C program to add two integers, you may use a variety of approaches, depending on the complexity and efficiency you prefer. The following list includes a few potential approaches for writing such a program:
- Using simple arithmetic
- Using user-defined functions
- Using a pointer-based function
- Using a macro
Approach 1: Using simple arithmetic
This method uses the addition operator to add the two numbers directly. It is necessary to define the numbers and output variable as an ‘int’ datatype in the main function before accepting the input, finding the sum of the numbers, and displaying the result.
C program:
#include <stdio.h>
int main() {
//declaring integers and output variables
int integer1, integer2, total=0;
//Read the values entered by the user
printf("\nEnter integer1:");
scanf("%d",&integer1);
printf("\nEnter integer2:");
scanf("%d",&integer2);
//Calculate the sum of integers
total=integer1+integer2;
//Display the sum of two integers
printf("The sum of two integers %d and %d is %d",integer1, integer2, total);
return 0;
}
Output:
![C Program to Add Two Integers](https://static.tutorialandexample.com/c/c-program-to-add-two-integers1.png)
Note:
- The printf function is used to display formatted output on the screen.
- The scanf function is used to read user inputs.
Approach 2: Using a user-defined function
This method executes the addition operation by explicitly declaring and defining an add function. The function accepts two numbers as input and returns the sum of the inputs.
C Program:
#include <stdio.h>
//declaring the add function
int add(int x, int y);
//defining the add function
int add(int x, int y){
return x+y;
}
int main() {
//declaring integers and output variables
int integer1, integer2, total=0;
// Read the values entered by the user
printf("\nEnter integer1:");
scanf("%d",&integer1);
printf("\nEnter integer2:");
scanf("%d",&integer2);
//Calling the add function
total=add(integer1, integer2);
//Display the sum of two integers
printf("The sum of two integers %d and %d is %d",integer1, integer2, total);
return 0;
}
Output:
![C Program to Add Two Integers](https://static.tutorialandexample.com/c/c-program-to-add-two-integers2.png)
Approach 3: Using a pointer-based function
The addition operation in this program is carried out by the add function, which takes three arguments: two pointers to integers (x and y) and one pointer to store the sum.
C Program:
#include<stdio.h>
//declaring add function
void add(int *x, int *y, int *sum);
//defining add function
void add(int *x, int *y, int *sum) {
*sum = *x + *y;
}
int main() {
//declaring integers and output variables
int integer1, integer2, total;
// Read the values entered by the user
printf("\nEnter integer1:");
scanf("%d",&integer1);
printf("\nEnter integer2:");
scanf("%d",&integer2);
//Calling the add function
add(&integer1, &integer2,&total);
//Display the sum of two integers
printf("The sum of two integers %d and %d is %d",integer1, integer2, total);
return 0;
}
Output:
![C Program to Add Two Integers](https://static.tutorialandexample.com/c/c-program-to-add-two-integers3.png)
Note:
- An address of other C variables or a memory location can be stored in a pointer, which is a derived data type. Pointers are used to access and modify the data kept in that memory address.
- ( * ) is dereferencing operator
Approach 4: Using a macro:
Any constant values or expressions are defined using macros, so they may be used anywhere in the code by just invoking the appropriate macro name. This approach uses a macro named ADD to carry out the addition operation. When given x and y as inputs, the macro expands to the equation (x + y).
C Program:
#include<stdio.h>
//defining a macro
#define ADD(x, y) (x + y)
int main() {
//declaring integers and output variables
int integer1, integer2, total;
// Read the values entered by the user
printf("\nEnter integer1:");
scanf("%d",&integer1);
printf("\nEnter integer2:");
scanf("%d",&integer2);
//Calling the add function
total=ADD(integer1,integer2);
//Display the sum of two integers
printf("The sum of two integers %d and %d is %d",integer1, integer2, total);
return 0;
}
Output:
![C Program to Add Two Integers](https://static.tutorialandexample.com/c/c-program-to-add-two-integers4.png)
Note:
- A macro is defined using the #define directive.
- The compiler substitutes the defined value or expression of the macro wherever a macro name appears in the code.