# C - Arithmetic Operators

### Arithmetic Operators

There are 2 kinds of arithmetic operators in C programming language based on the number of operands involved.

- Binary arithmetic operators
- Unary arithmetic operators

### Binary Operators

Binary operators work on 2 operands. List of some of the commonly used binary operators:

Operators | Operation | Operators Explanation | Syntax | Example |
---|---|---|---|---|

+ | Addition | Adds two operands | A + B | 50 + 40 = 90 |

- | Subtraction | Subtract second from first operands | A - B | 50 - 40 = 10 |

* | Multiplication | Multiplies two operands | A * B | 50 * 40 = 2000 |

/ | Division | Divide numerator by denominator | A / B | 50 / 40 = 1.25 |

% | Modulus Division | Return remainder after an integer division | A % B | 50 % 40 = 10 |

### Unary Operators

Unary operators work on a single operand. List of some of the commonly used unary operators:

Operators | Operation | Description | Example |
---|---|---|---|

+ | Unary Plus | Used on integer or floating point type | int a = +14; |

- | Unary Minus | Used on integer or floating point type | int a = -14; |

++ | Increment | Increase the integer value by one | A++; this is equivalent to A=A+1 |

-- | Decrement | Decrease the integer value by one | A--; this is equivalent to A=A-1 |

& | Address operator | Returns memory address of the variable | &A this will give the address of the variable A |

! | Logical complement | Changes the boolean value to its inverse | boolean inverseFlag=!true stores the value as false |

Note: The increment ++ and decrement - - operators may be used prefix or suffix alongh with the variable name.

- If they are used as prefix to the variable, then the preincrement or predecrement will be taken place.
Example: int x=2; int y= ++x; int Z=x;

In this case, the current value of x is first incremented by one and then assigned into y (i.e y=3). Hence, the z value is also 3.

- If they are used as suffix to the variable, then the postincrement or postdecrement will be taken place.
Example: int x=2; int y= x++; int Z=x;

In this case, the current value of x is assigned into y (i.e y=2). After that the x value is incremented by one that is 3. Hence, the z value is 3.

#### Example

#include<stdio.h> int main() { int a=30,C; int b=10; printf("The Addition of two integers (a+b) = %d\n",(a+b)); printf("The Subtraction of two integers(a-b) = %d\n ",(a-b)); printf("The Multiplies of two integers(a*b) = %d \n ",(a*b) ); printf("The Divition of two integers(a/b) = %d \n ",(a/b) ); C = b % a; printf("The Modulus of two integers: %d \n ",C); a++; printf("After Using increment operators a++ : %d \n",a); b--; printf("After Using decrement operators b-- : %d \n",b); return(0); }

Output:

The Addition of two integers (a+b) = 40 The Subtraction of two integers(a-b) = 20 The Multiplies of two integers(a*b) = 300 The Divition of two integers(a/b) = 3 The Modulus of two integers: 10 After Using increment operators a++ : 31 After Using decrement operators b-- : 9