Arithmetic Operators:
There are 5 arithmetic
operators in C computer language. These are given below-
+
|
Addition
|
-
|
Subtraction
|
*
|
Multiplication
|
/
|
Division
|
%
|
Modulus
|
These operators are used in
arithmetic operations. Modulus operator (%) is used to get remainder after
division between two numbers.
For
example:
int a = 10, b = 7;
int sum = a + b; /* gives 17 */
int sub = a – b; /* gives 3 */
int mul = a * b; /* gives 70 */
int div = a / b; /* gives 1 */
int md = a % b; /* gives 3 */
Variables or constants which
are operated upon by these operators are called operands. In the above examples, variables ‘a’ and ‘b’ are operands.
The combination of variables, constants, and operators is called ‘expression’. The equal sign ‘=’ is the assignment operator. It
assigns the value of an expression, or variable, or constant written on the
right hand side of it to the variable written on the left side of it. There
should be only one variable on the left hand side of ‘=’.
There are two types of
arithmetic instructions in C language as given below-
All the operands and
constants, and result variable are of the same type i.e. all are integers, or
all are floats.
For
example:
int a = 10, b = 6;
int sum = a + b; /* All are of integer type */
float a = 5.0, b = 7.0;
float mul = a * b; /* All are of float type */
Second types of arithmetic
instructions are those, which have a mix of integers and floats.
For
example:
int basic = 3000;
float da = 0.12 * basic;
float gross = basic + da;
Whenever there is a mix of
integers and floats, an automatic
conversion occurs and data types are promoted from ‘int’ to ‘float’ or
demoted from ‘float’ to ‘int’ depending on the case. In the
above case, ‘basic’ is of integer type, when it is multiplied by a floating
point number ‘0.12’, it is promoted to ‘float’,
and the result, which is of type ‘float’,
is assigned to float type variable ‘da’.
Same happens with the next statement.
Please
note:
- An arithmetic operation between two integers always results in an integer.
- An arithmetic operation between two floats always results in a ‘float’.
- An operation between a ‘float’ and ‘int’ always results in a ‘float’. ‘int’ is, first, promoted to ‘float’, and then the operation is carried out.
For
example:
7 / 2 = 3 /* operation between two ints gives the
result in ‘int’. */
2 / 7 = 0 /* operation between two ints gives the
result in ‘int’. */
7.0 / 2.0 = 3.5
/* operation between two floats gives
the result in ‘float’. */
2.0 / 7.0 = 0.285714
/* operation between two floats gives
the result in float. */
7.0 / 2 = 3.5 /* operation between a float and a int
results in a float */
7 / 2.0 = 3.5 /* operation between an int and a float
results in a float */
If result of the right hand
side expression is ‘int’ and left
hand side variable of assignment operator is ‘float’, then result of the right hand side expression is promoted
to ‘float’ and assigned to left hand
side variable.
If result of the right hand
expression is ‘float’ and left hand
side variable is ‘int’, then result
of right hand side expression is demoted to ‘int’ and assigned to left hand side variable.
For
example:
int a;
float b;
a = 7 / 2; /* result = 3 (7 and 2 are integers, result
is integer.) */
a = 7.0 / 2
/* result = 3 (7.0 is ‘float’. 2 is
integer, it is promoted to ‘float’, 2.0. Result of operation is float, which is
3.5. But a is ‘int’, it can’t contain ‘float’, so 3.5 is demoted to ‘int’ 3,
and stored in ‘a’.) */
a = 7.0 / 2.0 /* result = 3 */
b = 7 / 2
/* result = 3.0 (7 and 2, both are integers,
result is an integer, 3. But ‘b’ is float, so 3 is promoted to float 3.0 and
assigned to ‘b’.) */
b = 7.0 / 2
/* result = 3.5 (7.0 is a ‘float’, whereas 2
is an ‘int’. 2 is promoted to ‘float’, 2.0. Result of operation is float, 3.5. ‘b’
is float. 3.5 is assigned to ‘b’.) */
b = 7 / 2.0 /* result = 3.5 */
b = 7.0 / 2.0 /* result = 3.5 */
That’s all about arithmetic
operators and automatic type conversion.
If you are interested in
creating graphics using C language, please visit the blog Graphics in C.
No comments:
Post a Comment