You are here

C program to add two numbers using pointers

C program for the addition of two numbers using pointers. In the program, we have two integer variables x and y and two pointer variables p and q. We assign the addresses of x and y to p and q respectively and then assign the sum of x and y to the variable sum. Remember '&' is the address of operator and '*' is value at the address operator.

C program

#include <stdio.h>

int main()
{
   int first, second, *p, *q, sum;

   printf("Enter two integers to add\n");
   scanf("%d%d", &first, &second);

   p = &first;
   q = &second;

   sum = *p + *q;

   printf("Sum of the numbers = %d\n", sum);

   return 0;
}

Download Add integers using pointers program.

Output of program:
Add numbers using pointers

C program to add numbers using call by reference

#include <stdio.h>

long add(long *, long *);
 
int main()
{
   long first, second, *p, *q, sum;
 
   printf("Input two integers to add\n");
   scanf("%ld%ld", &first, &second);
 
   sum = add(&first, &second);
 
   printf("(%ld) + (%ld) = (%ld)\n", first, second, sum);
 
   return 0;
}

long add(long *x, long *y) {
   long sum;
   
   sum = *x + *y;
   
   return sum;
}