Different types of operators in c programming

Different types of operators in c programming

Posted by

Types of operators are the symbols which perform operation on operands. The operation may be arithmetic or comparison of data.

Z = x + y;

Here, x, y and z are operands and ‘+’ and ‘=’ are the operations.

Types of Operators

C++ programming is rich in operators and supports a wide range of these operators that range from the simplest unary to the complex ternary. Operators are very important because without their use, expressions cannot be evaluated.

The following are different types of operators in c programming.

1. Assignment = operator

The assignment operator assigns a value to a variable. The symbol = is used as an assignment operator. For example

VarA = 55;

This statement assigns the integer value 55 to the variable VarA. The part at the left of the assignment operator = is a variable whereas the right side can be a constant value, a variable, the result of an operation or any combination of these.

The assignment operation always takes place from right to left

VarA = VarB;

This statement assigns the value of variable VarB to VarA.

Consider the following program to demonstrate the use of assignment operator.


#include <iostream.h>
#include <conio.h>
int main()
{
int VarA, VarB;
VarA = 100;
VarB = 400;
cout<<”VarA :”<<VarA;
cout<<endl;
cout<<”VarB :”<<VarB;
getch();
return 0;
}

Output of the program

VarA : 100
VarB :400

2. Arithmetic operators

There are five arithmetic operators used in C++ programming. These are (+, -, *, /, %) that support the following arithmetic operations shown in the table below

+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus

The percentage sign % is used for modulus. Modulus is the operation that gives the remainder of a division of two values. For example, if we write:

remainder = 11 % 5;

The variable ‘remainder’ will contain the value 1, since 1 is the remainder from dividing 11 by 5. The following program demonstrates the use of these operators.

#include <iostream.h>
#include <conio.h>
int main()
{
int a = 11, b = 5;
int sum = a + b;
int difference = a – b;
int product = a * b;
float division = a / b;
int remainder = a % b;
cout << “sum =” << sum << endl;
cout << “difference =” difference << endl;
cout << “product =” product << endl;
cout << “division =” << division << endl;
cout << “remainder =” << remainder;
getch();
return 0;
}

Output of the program

sum = 16
difference = 6
product = 55
division = 2.0
remainder = 1

3. Compound Operators

Also called arithmetic assignment operators used in C++ are: (+=, -=, *=, /=, %=). These operators are used to modify the value of a variable by performing an operation on the value that is currently stored in that variable. For example, if we want to add the value of a variable “y” to the value of another variable “x” then the following statement is used.

x+=y;

i.e. x = x+y;

The list of these compound assignment operators along with their use are:

 

Operator Symbol Form Operation
Addition assignment += x += y Add y to x
Subtraction assignment -+ x -= y Subtract y from x
Multiplication assignment *= x *= y Multiply x by y
Division assignment /= x /= y Divide x by y
Modulus assignment %= x %= y Put the remainder of x/y in x

Consider the following example to demonstrate the use of compound assignment operators.

#include <iostream.h>
#include <conio.h>
int main()
{
int VarA, VarB = 300;
VarA = VarB;
VarA += 200;   // equivalent to VarA = VarA+200
cout << “Value after addition = ”<<VarA;
getch();
return 0;
}

Output of the program

Value after addition = 500

Increment and Decrement operators

These operators are also called unary arithmetic operators. These operators are used to make expressions short. The increment operator ++ and the decrement operator – increase or decrease the value stored in a variable by 1. For example the following statements are equivalent and all perform the same operation of increasing the value of the variable ‘VarC’ by 1.

VarC ++;
VarC += 1;
VarC = VarC + 1;

There are two versions of each of these increment and decrement operators. These are: a prefix version and a postfix version.

Prefix and Postfix forms

In prefix form of the increment and decrement operators, the operators precede their operands, e.g. ++x, --z

Consider the following segment of code:

//Prefix Increment and Decrement Operators

int x = 5;

cout<<”increment in prefix form =”<< ++x;

cout<<”decrement in prefix form =”<< --x;

Output of the segment

increment in prefix form = 6

decrement in prefix form = 4

In postfix form of the increment and decrement operators, the operands precede their operators, e.g. x++, z--

Consider the following program to demonstrate the use of increment and decrement operators.


// increment and decrement operators
#include <iostream.h>
#include <conio.h>
int main()
{
int x = 25, y = 25;
cout << x << “ ” << y << endl;
cout << ++x << --y <<endl;  //prefix
cout << x << “ “ y <<endl;
cout << x++ << “ “ << y-- <<endl;  // postfix
cout << x << y << endl;
getch();
return 0;
}

Output of the program

25 25
26 24
26 24
26 24
27 23

4. Relational operators

Relational operators are used for the comparison between two expression. These operators are: ==, !=, >, >=, <, <=. The result of these operators can be either true or false. The list of all relational operators used in C++ is given below

operator

Symbol

Form

Operation

Greater than > x > y True if x is greater than y, false otherwise
Less than < x < y True if x is less than y, false otherwise
Greater than or equals >= x >= y True if x is greater than or equal to y, false otherwise
Less than or equals <= x <= y True if x is less than or equal to y, false otherwise
Equality == x == y True if x equals y, false otherwise
Inequality != x != y True if x does not equal y, false otherwise

Consider the following lines of code:

(7 == 5)  //false
(5 > 4)   //true
(3 != 2)  //true
(6 >= 6)  //true
(5 < 5)    //false

5. Logical operators

There are three logical operators used in C plus plus.  these are: !, &&, ||  that are shown in the table below with their Operations.

Operator

Symbol

Form

Operation

Logical NOT ! !x True if x is false, or false if x is true
Logical AND && x && y True if both x and y are true, false otherwise
Logical OR || x || y True if either x or y are true, false otherwise

Logical NOT

The ! Operator is the C++ unary operator that is used to perform the Boolean NOT operation. The output of this operator is the inverse of the value of its operand. It produces false if its operand is true and true if its operand is false. The effects of logical NOT operator is summarized in the table below.

X

! X

 True   False
  False  True

The following lines of code demonstrate the working of NOT operator.


!( 5 == 5 ) //  evaluates to false because the expression (5 == 5) is true.
!(6 <= 4 ) //    evaluates to true because (6 <= 4 ) would be false.
!true // evaluates to false
!false // evaluates to true

Logical AND

logical && operator is a binary operator that needs two operands. it is used to test whether both conditions are true or not. if both the conditions are true, the logical && returns true, otherwise, it returns false.

X

Y

X && Y

False False False
False True False
True False False
True True True

Logical OR

The logical || operator is used to test whether either of two conditions is true. if either of the left operand or right operand evaluates to true, the logical || operator returns true. Also, if both the operands are true, logical || returns to true but if both are false it returns to false.

X

Y

X || Y

False False False
False True True
True False True
True True True

6. Ternary (or conditional) operator ( ?: )

The conditional operator takes three operands and is therefore also called ternary operator. It evaluates an expression and returns a value if that expression is true and a different value if the expression is false. the general syntax use for conditional operator is:

condition ? result1: result2;

Here, if condition is true the expression will return result1, and if condition is false it will return result2. Consider the following lines of code to demonstrate the use of conditional operator.


7 == 5 ?4 :3; // returns 3, since 7 is not equal to 5.
7 == 5+2 ?4 :3; //returns 4, since 7 is equal to 5+2.
5 > 3 ?a : b; // returns the value of a, since 5 is greater than 3.

The following program demonstrates the use of conditional operator.

// conditional operator
#includ <iostream.h>
#include <conio.h>
int main()
{
int x, y, z;
x = 22;
y = 77;
z = (x > y) ? x : y;
cout << "The greater value is: "<<z;
getch();
return 0;
}

Output of the program

The greater value is: 77

Leave a Reply

Your email address will not be published. Required fields are marked *