C Program to Add Two Numbers Using Pointers

Howdy readers, today you will learn how to write a program to add two numbers using pointers in the C Programming language.

C Program to Add Two Numbers Using Pointers

Pointers are special variables that are used to store addresses rather than values. In easy words, you can say that a pointer is a variable that stores the address of another variable.

NameOperatorDescription
Address Operator&Memory address of a variable
Dereference Operator*It returns the value of the variable located at the address specified the pointer

The below program prompts the user to enter two numbers, then we will assign those two numbers stored in the variables to pointers. After that, we add the values of pointers to find the sum of two numbers.

So, without any delay, let’s begin this tutorial.

C Program to Add Two Numbers Using Pointers

C Program

// C Program to Add Two Numbers Using Pointers
#include <stdio.h>

int main(){
    int a, b, sum;
    int *first, *second;
    
    // Taking input
    printf("Enter the first number: ");
    scanf("%d", &a);
    printf("Enter the second number: ");
    scanf("%d", &b);
    
    // Assigning pointers
    first = &a;
    second = &b;
    
    sum = *first + *second;
    
    // Displaying result
    printf("Sum of two numbers is: %d", sum);
    
    return 0;
}

Output

Enter the first number: 6
Enter the second number: 9
Sum of two numbers is: 15
C Program to Add Two Numbers Using Pointers

Explanation

    int a, b, sum;
    int *first, *second;

In this program, we have declared three int data type variables and two pointers named first and second respectively.

    // Taking input
    printf("Enter the first number: ");
    scanf("%d", &a);
    printf("Enter the second number: ");
    scanf("%d", &b);

This program prompts the user to enter two numbers.

    // Assigning pointers
    first = &a;
    second = &b;

We assign the address of the first number to the ‘first’ named pointer. Similarly, the address of the second number is assigned to the ‘second’ named variable.

Let’s understand the working of pointers by a simple example.

Int x, *firstpointer;
x = 7;
firstpointer = &x;

Here, 7 is assigned to the x variable. And, the address of x is assigned to firstpointer named pointer. The (*) is called the dereference operator when working with pointers. It operates on a pointer and returns the value stored in that pointer.

  sum = *first + *second;

Since, (*) dereference operator returns the value stored in the pointer, we use it to find the sum.

    // Displaying result
    printf("Sum of two numbers is: %d", sum);

Finally, the computed sum is displayed on the screen using printf() function.

Conclusion

I hope after going through this tutorial, you understand how to write a program to add two numbers using the C Programming language.

If you have any queries regarding the tutorial, then let us know in the comment section. We will be pleased to solve all of your queries.

Leave a Comment