C and C++ Arithmetic Operators, Increment, Decrement, Unary, Binary

<<Previous

Next >>





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:

OperatorsOperationDescriptionExample
++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
+Unary PlusUsed on integer or floating point typeint a = +14;
-Unary MinusUsed on integer or floating point typeint a = -14;
&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

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:

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

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

<< Previous

Next >>




strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length














C and C++ Arithmetic Operators, Increment, Decrement, Unary, Binary

<<Previous

Next >>





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:

OperatorsOperationDescriptionExample
++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
+Unary PlusUsed on integer or floating point typeint a = +14;
-Unary MinusUsed on integer or floating point typeint a = -14;
&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

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:

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

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

<< Previous

Next >>






strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy