Operators in C/C++ Language

Operators in C/C++ language can be defined as symbols that helps us to perform specific mathematical and logical computations on operands. The basic functionality of C/C++ languages is incomplete without the operators. C/C++ contains various sets of built-in operators to perform arithmetic, conditional and bitwise operations.

C/C++ operators are classified as:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Other Operators

Types of Operators in C/C++

Arithmetic Operators

These operators used to perform mathematical operations on operands or on numerical values (constants and variables) such as addition, subtraction, multiply, division etc.

Arithmetic operator are of two types namely Binary which operates on two operands and unary operates on single operand.

Binary operators

  • Assignment (‘=’): used to assign Lvalue (left-hand-side value) to Rvalue (right-hand-side value). for example, c = a+b;
  • Addition (‘+’): used to adds two operands or variables or numerical values. For example, a+b.
  • Subtraction (‘-’): The minus operator used to subtract two operands. For example, a-b.
  • Multiplication (‘*’): Used to multiply two operands. For example, a*b.
  • Division (‘/’): divides the first operand ‘a’ by the second operand ‘b’. For example, a/b.
  • Modulus (‘%’): Return the remainder when first operand is divided by the second. For example, a%b.
Arithmetic Operators in C/C++
Arithmetic Operators
// Binary Arithmetic operators example
int main()
{
int a = 6,b = 4;
int c;

c = a+b;
printf("Addition c = %d\n",c);

c = a-b;
printf("Subtract c = %d\n",c);

c = a*b;
printf("Multiply c = %d\n",c);

c = a/b;
printf("Divide c = %d\n",c);

c = a%b;
printf("Module c = %d\n",c);

return 0;
}
Output:
Addition c = 10
Subtract c = 2
Multiply c = 24
Divide c = 1
Module c = 2

Unary arithmetic operators

Increment (‘++’): ‘++’ operator used to increase the value of an integer or variable by one i.e. ++x is similar to x=x+1.

  • pre-increment: When ++ is placed before the variable is called pre-increment and its value is incremented instantly and then assigned or used. For example, ++x.
  • post-increment: when ++ is placed after the variable it is called post-increment and its value is first used than incremented for next statement. For example, x++.

Decrement: (‘–’): ‘–’ operator used to decrease the value of an integer or variable by one i.e. –x is similar to x=x-1.

  • pre-decrement: When — is placed before the variable is called pre-decrement and its value is decremented instantly and then assigned or used. For example, –x.
  • post-decrement: when — is placed after the variable it is called post-decrement and its value is first used than incremented for next statement. For example, x–.
// Increment and Decrement Example
int main()
{
int a = 6,b = 4;
int i,j,x,y;

i = ++a;
printf("Now i = %d and a = %d\n",i,a);
j = a++;
printf("Now j = %d and a = %d\n",j,a);

x = --b;
printf("Now x = %d and b = %d\n",x,b);
y = b--;
printf("Now y = %d and b = %d\n",y,b);

return 0;
} 
Output:
Now i = 7 and a = 7
Now j = 7 and a = 8
Now x = 3 and b = 3
Now y = 3 and b = 2 

Relational Operators

Relational operators used for comparison of the two operands. The result of this comparison is either true or false used mostly in loops, if-else conditions and more.

Some of the relational operators are:

  • Equivalent (==): Used to checks if the values of two operands are equal or not. If yes, then the condition becomes true and following statement executed else false.
  • Not equivalent (! =): Used to checks if the values of two operands are equal or not. If not equal, then the condition becomes true else false.
  • Less than (<): Used to checks if the values of one operand is less than other operand. If yes, then the condition becomes true else false.
  • Greater than (>): Used to checks if the values of one operand is greater than other operand. If yes, then the condition becomes true else false.
  • Less than or equal to (<=): Used to checks if the values of one operand is less or equal to than other operand. If yes, then the condition becomes true else false.
  • Greater than equal to (>=): Used to checks if the values of one operand is greater or equal to than other operand. If yes, then the condition becomes true else false.
// Relational Operators Example 
int main()
{
int a = 6,b = 4,c=4;

if(a==b)
    printf("a is equal to b\n");
else
    printf("a not equal b\n");


if(a!=b)
    printf("a not equal to b\n");
else
    printf("a is equal to b\n");


if(a<b)
    printf("a is less than b\n");
else
    printf("a is not less than b\n");


if(a>b)
    printf("a is greater than b\n");
else
    printf("a is not greater than b\n");

if(b<=c)
    printf("b is less than or equal to c\n");
else
    printf("b is greater than c\n");

if(b>=a)
    printf("b is greater than or equal to a\n");
else
    printf("b is less than a\n");

return 0;
} 

Output:

a not equal b
a not equal to b
a is not less than b
a is greater than b
b is less than or equal to c
b is less than a 

Logical Operators

Logical operators are mainly used to control program flow and combine two or more conditions. It is most often used to test whether a certain condition between the statements or value is true or false. Usually, you will find them as part of loops or with some other control statement.

Important Note: The logical operator expects its operands to be Boolean expressions (either 1 or 0) and returns a Boolean value. Whereas, Bitwise operator works on Integral (short, int, char, long etc.) values and return Integral value. If an integral value is used as an operand than a zero is considered as false and non-zero is considered as true.

They are classified in to 3 which are described below:

  • Logical AND (‘&&’): Returns true when both the conditions are true otherwise returns false. AND operator doesn’t evaluate second operand if first operand becomes false, this process is called Short-Circuiting.
  • Logical OR (‘||’): Returns true when one or both the conditions are true otherwise returns false. Similarly, to AND operator OR too doesn’t evaluate second operand when first operand becomes true.
  • Logical NOT (‘!’): Returns true if the condition false otherwise it returns true. For example,if x = 0, !x returns true.
// Logical Operators Example
int main()
{
int a=6, b=4, c = 4;

// true == 1 and false == 0

if (a>b && b==c)
    printf("a>b is true AND b==c is true, AND printed\n");// 1 && 1 = 1
else
    printf("Both condition not True\n");


if (a>b || b<c)
    printf("a>b is true OR b<c is false, OR printed\n");// 1 || 0 = 1
else
    printf("Neither a is greater than b nor b is less than c\n");

// Non-zero which treated as TRUE

if (!b) // b is TRUE but we are making it as not True which is FALSE
    printf("b is zero\n");
else
    printf("b is not zero");

    return 0;
} 

Output:

a>b is true AND b==c is true, AND statement printed
a>b is true OR b<c is false, OR printed
b is not zero 
Truth Table
Truth Table

The above truth table applicable to both Logical and Bit-wise Operators.

Bitwise Operators

Bitwise operators used to perform bit-level operations on the operands. Bitwise operation applied only on integral data types such as int, char, long but not on float or double values. The operands are first converted to bit-level and then calculation is performed. All mathematical operations performed at bit-level are faster than normal process.

Bitwise operators should not be used in place of logical operators because the output of logical operator is either 0 or 1 whereas bit-wise output is integral value.

There are six of these namely

  • Bitwise AND (‘&’): Takes two equal-length binary representations of operands and performs the logical AND on each pair of the corresponding bits. if bit in both value is 1 than resulting bit 1 (1×1=1) everything else is 0 i.e. (1 × 0 = 0 and 0 × 0 = 0).
  • Bitwise OR (‘|’): Takes two equal-length binary representations of operands and performs the logical OR on each pair of the corresponding bits. if bit in both value is 0 than resulting bit 0 (0 x 0 = 0) everything else is 1 i.e. (1 × 0 = 1 and 1 x 1 =1).
  • Bitwise XOR (‘^’): Takes two equal-length binary representations of operands and performs the logical exclusive OR on each pair of the corresponding bits. if bit in both value is same than resulting bit is 0 (0 x 0 = 0 or 1 x 1 =0) else is 1 i.e. (1 × 0 = 1 and 0 x 1 =1).
  • Bitwise NOT (‘~’): Complement perform logical negation on each bit, forming the ones’ complement of the given binary value. Bit which was 0 become 1 and 1 become 0. ~ operator on small number can be a big number if the result is stored in an unsigned variable and result may be negative number if result is stored in signed variable.
  • Bitwise Left shift (‘< <‘): shift bits of the operand towards left side leading to increase in value.
  • Bitwise Right shift (‘> >’): shift bits of the operand towards right side leading to decrease in value.
 // Bit-wise example
int main()
{
    unsigned int a = 6, b = 4; //  a = 00000110 , b = 00000100,

    printf("a = %d, b = %d\n", a, b);
    printf("a&b = %d\n", a&b);  //00000110 & 00000100 -> 00000100
    printf("a|b = %d\n", a|b);  //00000110 | 00000100 -> 00000110
    printf("a^b = %d\n", a^b);  //00000110 ^ 00000100 -> 00000010
    printf("~a = %d\n", ~a);    // 00000110 -> 11111001
    printf("b<<2 = %d\n", b<<2);  // 00000100 -> 00010000
    printf("b>>2 = %d\n", b>>2);  // 00000100 -> 00000001

    return 0;
} 

Output:

a = 6, b = 4
a&b = 4
a|b = 6
a^b = 2
~a = -7
b<<2 = 16
b>>2 = 1 

Short-Hand operators

These short-hand operators are not separate ones but the combination of assignment and one of above describe operators.

Operator name 	                   Syntax       Meaning     
		
Addition assignment 	           a += b 	a = a + b 
Subtraction assignment 	           a -= b 	a = a - b 
Multiplication assignment          a *= b 	a = a * b 
Division assignment 	           a /= b 	a = a / b 
Modulo assignment 	           a %= b 	a = a % b 
Bitwise AND assignment 	           a &= b	a = a & b 
Bitwise OR assignment 	           a |= b	a = a | b 
Bitwise XOR assignment 	           a ^= b	a = a ^ b 
Bitwise left shift assignment 	   a <<= b 	a = a << b 
Bitwise right shift assignment 	   a >>= b 	a = a >> b 

Other Operators

Beside all discussed above there are many important operators in C and C++.

Some of them are:

  1. Comma
  2. Ternary
  3. sizeof

Comma operator: comma used to separate variable and expressions. Comma acts as both operator and separator. The comma operator is a binary operator that evaluates its first expression and discards the result, then evaluates the second, and assign the value to variable. The comma operator has the lowest precedence of any C/C++ operator.

//Example
int main()
{
int x=1,y=2; //comma as saparator

int a = (6,4); //comma as operator
printf("a = %d\n",a);

int b;

b = (x+1,y+2); //comma as operator
printf("b = %d",b);


    return 0;
}

Output:

a = 4
b = 4

Ternary or conditional operator: Ternary operator is some sort of compound if-else statement representation Exp1 ? Exp2 : Exp3 . Condition Exp1 evaluated if its non-zero or true Exp2 will be returned else Exp3. This conditional operator has return type which always depend on Exp2 and convertibility of Exp3. If Exp3 not convertible, the compiler will throws an error.

int x = 1,y = 2;

cout<<(x==1?x:y)<<endl;
cout<<(x!=1?x:y)<<endl;

//OUTPUT
1
2

Sizeof operator: sizeof() is a compile time unary operator used to compute the size of operand. The result of sizeof is unsigned integral type which is denoted by size_t. sizeof can be applied to any data-type, such as integer, floating-point types, pointer types, or compound datatypes such as Structure, union etc.

int x = 1;

cout<<"Size of x = "<<sizeof(x)<<endl;
cout<<"Size of int = "<<sizeof(int)<<endl;
cout<<"Size of double = "<<sizeof(double)<<endl;

Output:

Size of x = 4
Size of int = 4
Size of double = 8

Leave a Reply

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