April 29, 2018

Arithmetic operators in C


   Arithmetic operators are used for numeric calculations. They are of two types:

  • Unary arithmetic operators
  • Binary arithmetic operators




Types of Arithmetic Operators
Types of Arithmetic Operators

Unary Arithmetic Operators

Unary operators require only one operand. There are two unary arithmetic operators:


Operator Meaning
+ Unary plus operator; indicates positive value (numbers are positive without this, however)
 - Unary minus operator; negates an expression


Example:

   +x       -y

In this case + operator has no effect on the number x but - sign changes the sign of the operand y.

Binary Arithmetic Operators

Binary operators require two operands. There are five binary arithmetic operators:


Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus



The modulus operator (%) finds the remainder of an integer division. This operator can be applied only to integer operands and cannot be used on float or double operands. Note that unary plus and unary minus operators are different from the addition and subtraction operators.


Integer Arithmetic

When both operands are integers, the resulting value is always an integer. Let us take two variables a and b. The value of a is 13 and b is 4, the results of the following operations are:

Expression Result
a + b 17
a - b 9
a * b 52
a / b 3 (decimal part truncates)
a % b 1 (Remainder after integer division)



After division operation the decimal part will be truncated and result is only integer part of quotient. After modulus operation the result will be remainder part of integer division. The second operand must be nonzero for division and modulus operations.

The following program demonstrates integer arithmetic.


#include <stdio.h>

int main(void)
{
    int a = 13, b = 4;

    printf("\na + b = %d", a + b);

    printf("\na - b = %d", a - b);

    printf("\na * b = %d", a * b);

    printf("\na / b = %d", a / b);

    /* To print a single % character in the console we have to write % two times. */

    printf("\na %% b = %d", a % b);

    return 0;
}


Output:
a + b = 17
a - b = 9
a * b = 52
a / b = 3
a % b = 1


While performing modulo division, the sign of the result is always the sign of the first operand (the dividend). Therefore,

 15 %  7 =  1
-15 %  7 = -1
 15 % -7 =  1
-15 % -7 = -1


Floating Point Arithmetic

When both operands are floating point type, the result is also floating point type. Let us take two variables a and b. The value of a is 13.4 and b is 4.2, the results of the following operations are:

Expression Result
a + b 17.600000
a - b 9.200000
a * b 56.280000
a / b 3.190476



The modulus operator % cannot be used with floating point numbers.

The following program demonstrates floating point arithmetic.


#include <stdio.h>

int main(void)
{
    double a = 13.4, b = 4.2;

    printf("\na + b = %f", a + b);

    printf("\na - b = %f", a - b);

    printf("\na * b = %f", a * b);

    printf("\na / b = %f", a / b);

    return 0;
}


Output:
a + b = 17.600000
a - b = 9.200000
a * b = 56.280000
a / b = 3.190476



Mixed Mode Arithmetic

When one operand is of integer type and the other is of floating type, the resulting value is floating type. Let us take two variables a and b. The value of a is 12 and b is 2.5, the results of the following operations are:

Expression Result
a + b 14.500000
a - b 9.500000
a * b 30.000000
a / b 4.800000



Sometimes mixed mode arithmetic can help in getting exact results. For example the result of expression 5/4 will be 1, since integer arithmetic is applied. If we want exact result we can make one of the operands float type. For example 5.0/4 or 5/4.0, both will give result 1.25.

The following program demonstrates mixed mode arithmetic.


#include <stdio.h>

int main(void)
{
    int a = 12;
    float b = 2.5;

    printf("\na + b = %f", a + b);

    printf("\na - b = %f", a - b);

    printf("\na * b = %f", a * b);

    printf("\na / b = %f", a / b);

    return 0;
}


Output:
a + b = 14.500000
a - b = 9.500000
a * b = 30.000000
a / b = 4.800000



next    Increment and Decrement Operators in C
top    Index
prev    C Operators and Expressions



No comments:

Post a Comment