ADVERTISEMENT
ADVERTISEMENT

Expressions in C with Examples

In C, an expression is a collection of variables, constants, and operators that, when evaluated, produces a value. Expressions can be used independently or as a component of longer sentences. Following are some examples of expressions:

  • Arithmetic Expressions
  • Relational Expressions
  • Logical Expressions
  • Conditional Expressions
  • Functional Calls

 Arithmetic Expressions

These include operations such as addition, subtraction, multiplication, and division. For example: x=10 and y=20

c = x + y
c = x - y
c = x * y
c = x / y

Relational Expressions

These compare two values and yield a Boolean result (either true or false). For example:x=10 and y=20

c = x < y
c = x > y
c = x <= y
c = x >= y
c = x == y
c = x != y

Logical Expressions

These combine relational expressions using logical operators such as AND (&&) and OR (||). For example: x=10 and y=20

c = (x < y) && (x > 0)
c = (x == y) || (y < 0)

Conditional Expressions

These use the ternary operator (? :) to choose one of two expressions based on a condition. For example: x=10 and y=20

c = (x < y) ? x : y

Function Calls 

These invoke a function and can be used as part of an expression. For example:  x=10 and y=20

max(x, y)
This will call the max() function with arguments x and y and return the maximum value.

 

Rules to follow when solving expressions in C

  1. Parentheses can be used to group subexpressions and specify the order of operations. Expressions inside parentheses are evaluated first.

  2. The arithmetic operators follow the standard order of operations: first perform any operations inside parentheses, then perform multiplications and divisions (working from left to right), and finally perform additions and subtractions (also working from left to right).

  3. The logical operators && and || are evaluated before the relational operators, which are evaluated before the assignment operator (=).

  4. Function calls are treated as a single entity and are evaluated before any surrounding operators.

  5. The ternary operator (? :) is evaluated after all other operators.

BODMAS is a mnemonic acronym that stands for:

  • Brackets
  • Orders (i.e., powers and roots)
  • Division and Multiplication (performed left to right)
  • Addition and Subtraction (performed left to right)

This acronym is often used to help remember the order of operations when solving mathematical expressions. In C, the order of operations follows the same general rules as in mathematics. Here are some examples of expressions using the BODMAS rules:

result = (2 + 3) * 5;          // Brackets: 2 + 3 is evaluated first
result = 10 / 2 * 3;           // Division and Multiplication: 10 / 2 is evaluated first
result = 1 + 2 * 3 - 4 / 2;    // Multiplication and Division: 2 * 3 and 4 / 2 are evaluated first
result = (1 + 2) * (3 - 4) / 5 // Multiple sets of brackets: (1 + 2) and (3 - 4) are evaluated first
result = 2 + 3 * 5;            // Multiplication: 3 * 5 is evaluated first
result = (2 + 3) * 5;          // Brackets: 2 + 3 is evaluated first
 

 


ADVERTISEMENT

ADVERTISEMENT