C and C++ Arithmetic Operators, Increment, Decrement, Unary, Binary
Arithmetic Operators in C
There are 2 kinds of arithmetic operators in C programming language based on the number of operands involved.
- Unary arithmetic operators
- Binary arithmetic operators
Unary Operators in C
Unary operators in C work on a single operand. List of some of the commonly used unary operators:
Operators | Operation | Description | Example |
---|---|---|---|
++ | 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 |
+ | Unary Plus | Used on integer or floating point type | int a = +14; |
- | Unary Minus | Used on integer or floating point type | int a = -14; |
& | 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 |
Increment and Decrement Operators in C
Note: We have 2 variants of increment ++ and decrement - - operators.
- If Increment and Decrement Operators are used as prefix to the variable, then pre-increment or pre-decrement happens
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, value of z is also 3.
Result: y = 3 z = 3
- If Increment and Decrement Operators are used as suffix to the variable, then the post-increment or post-decrement happens.
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.
Result: y = 3 z = 3
Increment and Decrement Operators in C - Example Program
#include<stdio.h> int main() { int a=30,C; int b=10; a++; printf("After Using increment operators a++ : %d \n",a); b--; printf("After Using decrement operators b-- : %d \n",b); return(0); }
Output:
After Using increment operators a++ : 31 After Using decrement operators b-- : 9
Binary Operators in C
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 |
Binary Operators in C - 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); 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