C - Arithmetic Operators

<<Previous

Next >>





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:

OperatorsOperationOperators ExplanationSyntaxExample
+AdditionAdds two operands A + B50 + 40 = 90
-SubtractionSubtract second from first operands A - B 50 - 40 = 10
*MultiplicationMultiplies two operandsA * B50 * 40 = 2000
/DivisionDivide numerator by denominatorA / B50 / 40 = 1.25
%Modulus DivisionReturn 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:

OperatorsOperationDescriptionExample
+Unary PlusUsed on integer or floating point typeint a = +14;
-Unary MinusUsed on integer or floating point typeint a = -14;
++IncrementIncrease the integer value by oneA++;
this is equivalent to A=A+1
-- DecrementDecrease 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 complementChanges 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

<< Previous

Next >>







C - Arithmetic Operators

<<Previous

Next >>





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:

OperatorsOperationOperators ExplanationSyntaxExample
+AdditionAdds two operands A + B50 + 40 = 90
-SubtractionSubtract second from first operands A - B 50 - 40 = 10
*MultiplicationMultiplies two operandsA * B50 * 40 = 2000
/DivisionDivide numerator by denominatorA / B50 / 40 = 1.25
%Modulus DivisionReturn 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:

OperatorsOperationDescriptionExample
+Unary PlusUsed on integer or floating point typeint a = +14;
-Unary MinusUsed on integer or floating point typeint a = -14;
++IncrementIncrease the integer value by oneA++;
this is equivalent to A=A+1
-- DecrementDecrease 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 complementChanges 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

<< Previous

Next >>







Learn about Hashing and Hash table

Learn Expression Tree

OS Kernel Architecture

OS Arcitecture of Windows, Linux and MacOS

Learn OS scheduling Algorithms