Arithmetic Operators in C: A Comprehensive Guide
Learn about the fundamental building blocks of mathematical operations in C programming. This guide will cover the various arithmetic operators, including addition, subtraction, multiplication, division, modulus, increment, and decrement. Understand their usage, precedence, and associativity to effectively perform calculations in your C programs.
Arithmetic Operators
Arithmetic operators in C perform basic math operations. Here are the operators:
Operator | Description |
---|---|
+ | Adds two operands. |
- | Subtracts second operand from the first. |
* | Multiplies both operands. |
/ | Divides numerator by denominator. |
% | Modulus Operator; remainder of division. |
++ | Increments the integer value by one. |
-- | Decrements the integer value by one. |
Example: Arithmetic Operators
Syntax
#include <stdio.h>
int main() {
int op1 = 10;
int op2 = 3;
printf("Operand1: %d Operand2: %d\\n\\n", op1, op2);
printf("Addition of op1 and op2: %d\\n", op1 + op2);
printf("Subtraction of op2 from op1: %d\\n", op1 - op2);
printf("Multiplication of op1 and op2: %d\\n", op1 * op2);
printf("Division of op1 by op2: %d\\n", op1 / op2);
return 0;
}
Output
Operand1: 10 Operand2: 3
Addition of op1 and op2: 13
Subtraction of op2 from op1: 7
Multiplication of op1 and op2: 30
Division of op1 by op2: 3
Type Casting
Integer division returns an integer. For floating-point division, at least one operand should be a float.
Syntax
#include <stdio.h>
int main() {
int op1 = 10;
float op2 = 2.5;
printf("Operand1: %d Operand2: %f\\n", op1, op2);
printf("Addition of op1 and op2: %f\\n", op1 + op2);
printf("Subtraction of op2 from op1: %f\\n", op1 - op2);
printf("Multiplication of op1 and op2: %f\\n", op1 * op2);
printf("Division of op1 by op2: %f\\n", op1 / op2);
return 0;
}
Output
Operand1: 10 Operand2: 2.500000
Addition of op1 and op2: 12.500000
Subtraction of op2 from op1: 7.500000
Multiplication of op1 and op2: 25.000000
Division of op1 by op2: 4.000000
Arithmetic Operations with Char Data Type
The char data type can be used in arithmetic operations as it is a subset of int.
Syntax
#include <stdio.h>
int main() {
char op1 = 'F';
int op2 = 3;
printf("operand1: %c operand2: %d\\n", op1, op2);
printf("Addition of op1 and op2: %d\\n", op1 + op2);
printf("Subtraction of op2 from op1: %d\\n", op1 - op2);
printf("Multiplication of op1 and op2: %d\\n", op1 * op2);
printf("Division of op1 by op2: %d\\n", op1 / op2);
return 0;
}
Output
operand1: F operand2: 3
Addition of op1 and op2: 73
Subtraction of op2 from op1: 67
Multiplication of op1 and op2: 210
Division of op1 by op2: 23
Modulo Operator
The modulo operator (%) returns the remainder of a division operation.
Syntax
#include <stdio.h>
int main() {
int op1 = 10;
int op2 = 3;
printf("Operand1: %d Operand2: %d\\n", op1, op2);
printf("Modulo of op1 and op2: %d\\n", op1 % op2);
return 0;
}
Output
Operand1: 10 Operand2: 3
Modulo of op1 and op2: 1
Negation Operator
The "-" symbol can also act as a unary negation operator.
Syntax
#include <stdio.h>
int main() {
int op1 = 5;
int op2 = -op1;
printf("Operand1: %d Operand2: %d\\n", op1, op2);
return 0;
}
Output
Operand1: 5 Operand2: -5