C Subtraction Operator
In C Programming, Subtraction Operator is used to find the difference between two numbers. The operator takes two operands and returns the difference of second operand from first operand.
In this tutorial, we shall learn about Arithmetic Subtraction Operator and how to use this operator with values of different datatypes using example programs.
Operator Symbol
The symbol of Arithmetic Subtraction Operator is -
.
Syntax
The syntax of Subtraction Operator with two operands is
operand1 - operand2
When this operator is given with operands of different numeric datatypes, the lower datatype is promoted to higher datatype.
Examples
In the following program, we take two integers in a
, b
, and find the difference a - b
.
main.c
#include <stdio.h>
int main() {
int a = 12;
int b = 7;
int result = a - b;
printf("Result : %d\n", result);
return 0;
}
Output
Result : 5
Program ended with exit code: 0
In the following program, we take two floating point numbers in a
, b
, and find the difference a - b
.
main.c
#include <stdio.h>
int main() {
float a = 1.2;
float b = 0.7;
float result = a - b;
printf("Result : %f\n", result);
return 0;
}
Output
Result : 0.500000
Program ended with exit code: 0
In the following program, we initialize an integer value in b
, and a floating point value in a
, and find the difference a - b
.
main.c
#include <stdio.h>
int main(int argc, const char * argv[]) {
float a = 3.4;
int b = 2;
float result = a - b;
printf("Result : %f\n", result);
return 0;
}
Output
Result : 1.400000
Program ended with exit code: 0
Conclusion
In this C Tutorial, we learned how to use Arithmetic Subtraction Operator to find the difference between numeric values with examples.