C Program to Subtract Two Numbers

In this article, we will write a C program to subtract two numbers. The program takes the two numbers from the user as input, subtracts them, and prints the result on the output screen.

Sample Example:

Enter the first number: 40
Enter the second number: 10

The difference of two numbers is: 30

To subtract two numbers in C programming language, we use the subtraction operator(-). The subtraction operator takes two operands and returns their subtraction as a result.

The following C program shows how you can subtract two numbers:

// C Program to subtract two numbers

#include <stdio.h>

int main() {
	int num1, num2, diff;
	printf("Enter the first number: ");
	scanf("%d", &num1);
	printf("Enter the second number: ");
	scanf("%d", &num2);
	// Subtract both numbers and store result in the diff variable
	diff = num1 - num2;
	// Print the result
	printf("The difference of two numbers is: %d", diff);
	return 0;



Enter the first number: 40
Enter the second number: 10
The difference of two numbers is: 30

Program Explanation:

In the above program, we have declared three variables num1, num2 and diff of type int. The first two variables will be used to store the numbers entered by the user and the third variable diff will be used to store the result of the subtraction.

When the program runs, it prompts the user to enter two numbers. The first number entered by the user is stored in the num1 variable and the second number is stored in the num2 variable.

As the variables are of type int, therefore we have used the %d format specifier.

After taking the numbers from the user, we calculate their difference using the below statement:

diff = num1 - num2;

Finally, the result of subtraction is printed on the screen using the printf() function.

I hope you will find this article helpful. Thanks for reading.


  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts

Leave a Comment