C and C++ - Operators, Precedence 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 is carried out on the operands by the operator.
  • Expression: simply the statement that contains any string of operators, variables and numbers.

C Operators - Examples

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

C Programming - 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.

C Programming - Operators Precedence in Expressions

Let us consider an expression.

X = 2 + 5 * 4

This above expression can be evaluated in two different ways.

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

Two different results came from the above evaluation. Now, we cannot say that which one is correct. In order to resolve this confusion, we can assign different priory for different operators. The way in which operators are given priority in C programming language is known as Operator Precedence. Operator precedence defines that some operators have highest priory than the other and that highest priority operators will be evaluated first. Therefore, the correct result of an above expression in C programming 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. C language allow us to group the operations by using parentheses.

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

Note: On Mobile devices, Please scroll the table to the right to read
OperatorOperationsAssociativity Notes
( )
[ ]
 ->   .
Parentheses,
square brackets,
arrow and dot
left to rightused for
expression
grouping
! ++
 --  -  +  
(cast)  *  &   ;
sizeof
Logic NOT, increment,
decrement, minus, plus,
type cast, pointer, address and
sizeof
right to leftall unary
*   /   %Multiplication
Division and Modulus
left 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
=   +=   -=   *= ;
/=   %=   |= ;
  &=   ^=
Assignment right to leftbinary
,commaleft to rightbinary

<< Previous

Next >>




strcat() - String Concatenation        strcmp() - String Compare


strcpy() - String Copy        strlen() - String Length














C and C++ - Operators, Precedence 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 is carried out on the operands by the operator.
  • Expression: simply the statement that contains any string of operators, variables and numbers.

C Operators - Examples

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

C Programming - 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.

C Programming - Operators Precedence in Expressions

Let us consider an expression.

X = 2 + 5 * 4

This above expression can be evaluated in two different ways.

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

Two different results came from the above evaluation. Now, we cannot say that which one is correct. In order to resolve this confusion, we can assign different priory for different operators. The way in which operators are given priority in C programming language is known as Operator Precedence. Operator precedence defines that some operators have highest priory than the other and that highest priority operators will be evaluated first. Therefore, the correct result of an above expression in C programming 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. C language allow us to group the operations by using parentheses.

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

Note: On Mobile devices, Please scroll the table to the right to read
OperatorOperationsAssociativity Notes
( )
[ ]
 ->   .
Parentheses,
square brackets,
arrow and dot
left to rightused for
expression
grouping
! ++
 --  -  +  
(cast)  *  &   ;
sizeof
Logic NOT, increment,
decrement, minus, plus,
type cast, pointer, address and
sizeof
right to leftall unary
*   /   %Multiplication
Division and Modulus
left 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
=   +=   -=   *= ;
/=   %=   |= ;
  &=   ^=
Assignment right to leftbinary
,commaleft to rightbinary

<< Previous

Next >>






strncat() - String n Concatenation        strlwr() - String Lower       

strncmp() - String n Compare       strncpy() - String n Copy