PDF - CodeQuotient
PDF - CodeQuotient
TUTORIAL
C - Arithmetic Operators
Chapter
1. C - Arithmetic Operators
Lot of operators are defined in C language. Basically, operators are divided into four sections: arithmetic, relational, logical, bitwise. There are other special operators also like assignment operator.
Arithmetic Operators: -
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
-- Decrement
++ Increment
+ and – comes in two variants as they can be used as unary operators and binary operators both. In case of unary - acts as returning the negative value, which is used to represent negative values for example,
int b = 5;
int a = -b; // a will be -5 now.
+, -, *, / will act in usual behavior, returning addition, subtraction, multiplication and division (quotient) of two operands. % (modulus) returns the division (remainder) instead quotient. For example,
1 #include <stdio.h> C
2
3 int main()
4 {
5 int a,b,c,d;
6 a=5;
7 b=2;
8 c=1;
9 d=2;
10 printf("a=%d b=%d c=%d d=%d \n\n",a,b,c,d);
11 printf("a+b=%d\n", a + b);
12 printf("a-b=%d\n", a - b);
13 printf("a*b=%d\n", a * b);
14 printf("a/b=%d\n", a / b);
15 printf("a%%b=%d\n\n", a % b);
16
17 printf("c+d=%d\n", c + d);
18 printf("c-d=%d\n", c - d);
19 printf("c*d=%d\n", c * d);
20 printf("c/d=%d\n", c / d);
21 printf("c%%d=%d\n", c % d);
22
23 return 0;
24 }
25
++ and – are two operators which are required for quick addition and subtraction. If we need unit increment or decrement, we can use these operators. For example,
int x=10;
x = x + 1; // x will be 11 now onwards.
int y=10;
y++; // y will be 11 now onwards.
int a = 5;
a--; // a will be 4 now onwards.
int b =5;
b = b – 1; // b will be 4 now onwards.
Both increment and decrement operators can either be prefix to operand or suffix to operand, in those case, there are called as,
They are logically do the same thing as stated above, but when they used in expressions they behave differently. For example,
1 int a=5; C
2 int b=a++;
3
1 int a=5; C
2 int b=a; // returns the value first.
3 a = a + 1; // After returning, do the increment.
4
1 int a =5; C
2 int b = ++a;
3
1 int a = 5, b; C
2 a = a + 1; // Do the increment first.
3 b = a; // After increment return the value.
4
Obviously b will get different values in both cases. If post-increment case, b will use the old value of a (i.e. 5), and in pre-increment case b gets the new value of a (i.e. 6). But in both cases a will be 6 after execution. So both affects a in same manner (increment by 1), but they affect the expression in which they
are written. Same is the effect of post-decrement and pre-decrement operators.
In C, when we try to mix these operators too much in a single expression, that expression may yield different output on different compilers. For example,
This kind of statement has no specified order of evaluation for its execution in standard C. So depending on different implementations of C (different compiler of C) we may get different outputs. So in general, use them till they helps you, and it's easy to interpret. Otherwise the potability of your code may lose.