Showing posts with label Operators in C. Show all posts
Showing posts with label Operators in C. Show all posts

Saturday, 30 December 2017

Interesting facts about Operator Precedence and Associativity in C Language

Operator precedence decides which operator’s operation should be performed first in an expression with more than one operators with different precedence.

For example 8+ 2 * 10 is calculated as 8 + (2 * 10) and not as (8 + 2) * 10.

Associativity in C is used when two operators of same precedence present in an expression. Associativity can be either Left to Right or Right to Left.

For example ‘*’ and ‘/’ have same precedence and their associativity is Left to Right, so the expression “100 / 10 * 10” is treated as “(100 / 10) * 10”.

Precedence and Associativity are two characteristics of operators that determine the evaluation order of sub expressions in absence of brackets.
  • Associativity is used only when there are two or more operators of same precedence. Associativity doesn’t define the order in which operands of a single operator are evaluated. 
       For example associativity of the * operator is high so 2*3 performed first that 6+1 that is 7 assigned to a.          int a = 1+2*3;
  • All operators with same precedence have same associativity. This is necessary, otherwise there won’t be any way for compiler to decide evaluation order of expressions which have two operators of same precedence and different associativity. 
      For example + and – have same associativity.
  • Precedence and associativity of postfix ++ and prefix ++ are different
  • Precedence of postfix ++ is more than prefix ++, their associativity is also different. Associativity of postfix ++ is left to right and associativity of prefix ++ is right to left. 
Click here for more on Pre/post increments.
  • Comma has the least precedence among all operators and should be used carefully For example consider the following program, the output is 1.
#include
int main()
{
int a;
a = 1, 2, 3; // Evaluated as (a = 1), 2, 3
printf("%d", a);
return 0;
}


 Output:
1

Sunday, 24 December 2017

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"));

Geeks4Coding

Contributors

Popular Posts