C - Operators and Expression

<<Previous

Next >>





C supports various operators that can perform different operations. An operator is a symbol that is used to perform the mathematical and logical operations. By using an operator, we can link the numbers, variables and constants to form an expression. The following terms are used in c programming:

  • Operator: Operators are those which operate on one or more operands. Example: +, -, /, *, < ..etc.
  • Operand: data item on which operators perform the operations.
  • Operation: action which was carried out on the operands by the operator.
  • Expression: simply the statement that contains any string of operators, variables and numbers.

Example

ExpressionOperandOperatorsOperation
a+d-ea, d, & e+      -Addition & Subtraction
k%4 +h/bk, 4, h, and b%      + /Division, Modulus & Addition
a>ba, b>Comparison to check a Greater than condition
a<b && x<za, b, x, z<      &&Comparison to check multiple conditions
a=ba, b =value of b is assigned to a
x=(a>b)?10:5x,a,b,10,5=      >      ?:if a is greater than b, then 10 is stored in x. Otherwise, 5 is stored in x

Type of operators

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Conditional Operator
  • Comma Operator

In next pages, we will discuss each operator in detail.

Operators Precedence

Let us consider an expression.

X=2+5*4

This above expression can be evaluated in two different ways.

  • To perform addition and then multiplication,we will get 28 as a result.
    X=7*4=28
    
  • To perform multiplication and then addition, now we will get 22 as result.
    X=2+20=22
    

Finally, two kinds of result came from those evaluation. Now we cannot say that which one is correct. We are in confusion stage. In order to resolve this confusion, we can assign different priory for different operators. This is known as Operator Precedence. Operator precedence defines that some operators are having highest priory than other and that highest priority operators will be evaluated first . Therefore the correct result of an above expression is 22.

The associativity defines the operator direction in which the expression is to be evaluated. That means, the expressions are evaluated from left to right or right to left. This is also allow us to group the operators by using parentheses.

The following table shows the operators precedence from highest priority at the top to lowest priority at the bottom.

OperatorOperationsAssociativity Notes
( ) [ ]  ->   .Parentheses, square brackets, arrow and dotleft to rightfor expression grouping
! ++  --  -  +  (cast)  *  &   sizeofLogic NOT, increment, decrement, minus, plus,type cast, pointer, address and sizeofright to leftall unary
*   /   %Multiplication Division and Modulusleft to rightbinary
+   -Addition and Subtractionleft to rightbinary
<<    >>shift left to rightbinary
<  <=  >  >=Relationalleft to rightbinary
==    != Equalityleft to rightbinary
& Bitwise ANDleft to rightbinary
^Bitwise XORleft to rightbinary
|Bitwise OR left to rightbinary
&&Logic ANDleft to rightbinary
||Logic ORleft to rightbinary
? :Conditionalright to leftunusual
=   += and all combined assignment Assignment right to leftbinary
,commaleft to rightbinary

<< Previous

Next >>







C - Operators and Expression

<<Previous

Next >>





C supports various operators that can perform different operations. An operator is a symbol that is used to perform the mathematical and logical operations. By using an operator, we can link the numbers, variables and constants to form an expression. The following terms are used in c programming:

  • Operator: Operators are those which operate on one or more operands. Example: +, -, /, *, < ..etc.
  • Operand: data item on which operators perform the operations.
  • Operation: action which was carried out on the operands by the operator.
  • Expression: simply the statement that contains any string of operators, variables and numbers.

Example

ExpressionOperandOperatorsOperation
a+d-ea, d, & e+      -Addition & Subtraction
k%4 +h/bk, 4, h, and b%      + /Division, Modulus & Addition
a>ba, b>Comparison to check a Greater than condition
a<b && x<za, b, x, z<      &&Comparison to check multiple conditions
a=ba, b =value of b is assigned to a
x=(a>b)?10:5x,a,b,10,5=      >      ?:if a is greater than b, then 10 is stored in x. Otherwise, 5 is stored in x

Type of operators

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Conditional Operator
  • Comma Operator

In next pages, we will discuss each operator in detail.

Operators Precedence

Let us consider an expression.

X=2+5*4

This above expression can be evaluated in two different ways.

  • To perform addition and then multiplication,we will get 28 as a result.
    X=7*4=28
    
  • To perform multiplication and then addition, now we will get 22 as result.
    X=2+20=22
    

Finally, two kinds of result came from those evaluation. Now we cannot say that which one is correct. We are in confusion stage. In order to resolve this confusion, we can assign different priory for different operators. This is known as Operator Precedence. Operator precedence defines that some operators are having highest priory than other and that highest priority operators will be evaluated first . Therefore the correct result of an above expression is 22.

The associativity defines the operator direction in which the expression is to be evaluated. That means, the expressions are evaluated from left to right or right to left. This is also allow us to group the operators by using parentheses.

The following table shows the operators precedence from highest priority at the top to lowest priority at the bottom.

OperatorOperationsAssociativity Notes
( ) [ ]  ->   .Parentheses, square brackets, arrow and dotleft to rightfor expression grouping
! ++  --  -  +  (cast)  *  &   sizeofLogic NOT, increment, decrement, minus, plus,type cast, pointer, address and sizeofright to leftall unary
*   /   %Multiplication Division and Modulusleft to rightbinary
+   -Addition and Subtractionleft to rightbinary
<<    >>shift left to rightbinary
<  <=  >  >=Relationalleft to rightbinary
==    != Equalityleft to rightbinary
& Bitwise ANDleft to rightbinary
^Bitwise XORleft to rightbinary
|Bitwise OR left to rightbinary
&&Logic ANDleft to rightbinary
||Logic ORleft to rightbinary
? :Conditionalright to leftunusual
=   += and all combined assignment Assignment right to leftbinary
,commaleft to rightbinary

<< Previous

Next >>







Learn about Hashing and Hash table

Learn Expression Tree

OS Kernel Architecture

OS Arcitecture of Windows, Linux and MacOS

Learn OS scheduling Algorithms