C language: What are Expressions in C programming

expressions in c language

An Expression represents data items such as variables, constants, and are interconnected with operators as per the syntax of the language. An expression is evaluated using the assignment operator of the C language.


Syntax: variable = expression

Description: Any valid variable and expression

Example: x = a*b-c


In the above statement, the expression is evaluated first from left to right. After the evaluation of the expression, the final value is assigned to the variable from right to left.

Example: y = (a/b) + c;

                  z = (a*b) – c;

The blank spaces around an operator are optional and add only to improve the readability of the program. The variables that are used in an expression need to declare at the declaration part of the program.

Example of Algebric expression and C language expression

ALGEBRIC EXPRESSIONC EXPRESSION
a + b x ca + b * c
ax2 + bx + ca * x * x + b * x + c
(4ac/2a)(4 * a *c / 2 *a)
(2x2 / b) – c(2 * x * x) /b) – c

Operator Precedence in C language

In C programming language Precedence is used to determine the order in which different operators in a complex expression are evaluated.

Arithmetic operators Precedence

Usually, the Arithmetic operators are evaluated from left to right using the precedence when the expression is written without the parameters.

The Arithmetic Operator precedence

precedenceOperators
High* / %
Low+ –

Example:
result = x – y/3 + z * 3 – 1

                 x = 3, y = 9, z =77

sum = 3 – 9/3 + 77 * 3 – 1

Operator precedence in c language

during the evaluation of the expression, the order of evaluation can be changed by putting the parenthesis in an expression.

Example:

              (3 – 9) / 3 + 77 * (3 – 1)

              – 6/3 + 77 * (3 – 1)

              – 6/3 + 77 * (3 -1)

               -2 + 154

              = 152

Rules for evaluation of an expression in c language

  • Evaluate the sub-expressions from left to right if parenthesized.
  • Evaluate the arithmetic expression from left to right using the rules of precedence.
  • The highest precedence is given to the expressions within parenthesis.
  • Apply the associative rule, if more operators of the same precedence occur.
  • Evaluate the innermost sub-expression if the parenthesis is nested.

Every operator in the C programming language has precedence associated with it. The precedence is used to determine how an expression involving more than one operator is evaluated.

 The operators at the higher level of precedence are evaluated first. The operators of the same precedence evaluated either from left to right or right to left depending on the level is known as associativity.

The hierarchy of operators in ‘C’ are summarised below

  1. Any expression within parenthesis is first evaluated if more than one pair of parenthesis are present, the innermost parenthesis is evaluated first.
  2. Unary operators are evaluated first in an expression.
  3. Then priority is given to multiplication and expression.
  4. Then subtraction and addition are performed.
  5. Then relational operations are performed.
  6. Then quality checking is performed.
  7. Then logical operations are performed.
  8. Then the operations are checked.
  9. Finally, the assignment operation is carried out.

FAQ's