How to Subtract two numbers using pointers

In this post, we are going to learn how to Subtract two numbers using pointers with examples

How to Subtract two numbers using pointers


In this c program, we are asking users to input two numbers and store them into pointer variables and using pointer variables to subtract two numbers using pointe.

#include <stdio.h>

int main()
{
// Declare 2 numbers
    int num1, num2;
    int	Sub;
// Declare 2 integer pointers
    int *ptr1, *ptr2;
	
// Assign numbers address to pointers
    ptr1 = &num1; 
    ptr2 = &num2; 

    printf("Please enter two numbers for Substraction: ");
    scanf("%d%d", ptr1, ptr2);

    Sub = *ptr1 - *ptr2;

    printf("The Substraction results are = %d", Sub);

    return 0;
}

Output

Please enter two numbers for Substraction: 5
10
The Substraction results are = -5