Sunday 24 December 2017

Operators in C and C++ Programming Languages - Geeks4Coding

Operators in C and C++

Operators are the foundation of any programming language. Thus the functionality of C language is incomplete without the use of operators. Operators allow us to perform different kinds of operations on operands. In C, operators in Can be categorized in following categories:

  • Arithmetic Operators (+, -, *, /, %, post-increment, pre-increment, post-decrement, pre-decrement)
  • Relational Operators (==, != , >, <, >= & <=) Logical Operators (&&, || and !) 
  •  Bitwise Operators (&, |, ^, ~, >> and <<) • Assignment Operators (=, +=, -=, *=, etc) 
  • Other Operators (conditional, comma, sizeof, address, redirecton) 

Arithmetic Operators: These are used to perform arithmetic/mathematical operations on operands.

The binary operators falling in this category are:

  • Addition: The ‘+’ operator adds two operands. For example, x+y. 
  • Subtraction: The ‘-‘ operator subtracts two operands. For example, x-y. 
  • Multiplication: The ‘*’ operator multiplies two operands. For example, x*y. 
  • Division: The ‘/’ operator divides the first operand by the second. For example, x/y. 
  • Modulus: The ‘%’ operator returns the remainder when first operand is divided by the second. For example, x%y. 
 The ones falling into the category of unary arithmetic operators are:

Increment: The ‘++’ operator is used to increment the value of an integer. When placed before the variable name (also called pre-increment operator), its value is incremented instantly. For example, ++x. And when it is placed after the variable name (also called post-increment operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, x++.

Decrement: The ‘–‘ operator is used to decrement the value of an integer. When placed before the variable name (also called pre-decrement operator), its value is decremented instantly. For example, –x. And when it is placed after the variable name (also called post-decrement operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, x–.

Relational Operators: 

Relational operators are used for comparison of two values. Let’s see them one by one:

  •  ‘==’ operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example, 1==1 will return true. 
  • ‘!=’ operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 1!=1 will return false. 
  • ‘>’ operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false. For example, 8>5 will return true.
  • ‘<‘ operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. For example, 8<5 operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise it returns false.
  • ‘>=’ operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5>=5 will return true.
  • ‘<=’ operator checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5<=5 will also return true.


Logical Operators:

They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. They are described below:

Logical AND: The ‘&&’ operator returns true when both the conditions in consideration are satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).

Logical OR: The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b is true (i.e. non-zero). Of course, it returns true when both a and b are true.

Logical NOT: The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.

Short-Circuiting in Logical Operators:


In case of logical AND, the second operand is not evaluated if first operand is false. For example,  below doesn’t print “Geeks4Coding” as the first operand of logical AND itself is false.

bool res = ((a == b) && printf("Geeks4Coding"));

In case of logical OR, the second operand is not evaluated if first operand is true. For example,  below doesn’t print “Geeks4Coding” as the first operand of logical OR itself is true.

int a=10, b=4;
bool res = ((a != b) || printf("Geeks4Coding"));

No comments:

Post a Comment

Thank you for your Time. Keep Learning

Geeks4Coding

Contributors

Popular Posts