Sample C Program to Add Two Numbers using Pointers

How to write a Sample C Program to Add Two Numbers using Pointers and print the output? I suggest you refer to Pointers Introduction to understand the basics of pointers.

Sample C Program to Add Two Numbers using Pointers

This C program lets the user enter two integer values. Then we are going to add those two numbers using the concept of the pointer. Later we will assign the total to a variable sum.

#include <stdio.h>
int main()
{
  int number1, number2, sum;
  int *pntr1, *pntr2;
  
  pntr1 = &number1;
  pntr2 = &number2;
 
  printf(" Please Enter two integer values : \n ");
  scanf("%d  %d", pntr1, pntr2);
  
  sum = *pntr1 + *pntr2;
 
  printf(" The Sum of two integer values is = %d", sum);
  return 0;
}
Sample C Program to Add Two Numbers using Pointers 1

First, we declared three integer values called number1, number2, and sum. And then, we declared two Pointer variables of type integer.

  int number1, number2, sum;
  int *pntr1, *pntr2;

Next, we are assigning the address of the number1 number2 to pointers pntr1, pntr2.

  pntr1 = &number1;
  pntr2 = &number2;

The following statements ask the user to enter two integer numbers. The next C Programming scanf statement will assign the user entered values to already declared pointer variables pntr1, pntr2. Here, pntr1 and pntr2 are the address location of number1 and number2.

  printf(" Please Enter two integer values : \n ");
  scanf("%d  %d", pntr1, pntr2);

Next line of this C program, we used Arithmetic Operators + to add the integer values and then assigned that total to the sum.

sum = *pntr1 + *pntr2;

Following printf statement will print the sum variable as output (22 + 44 = 66).

printf(" The Sum of two integer values is = %d", sum);