Detailed Explanation of Expressions in C Language

An expression is a formula in which operands are connected together using operators to compute a value. Operands can be function references, variables, array elements, or constants.

Let’s look at an example:

a-b;

In the above expression, the minus sign (-) is an operator, and a and b are two operands.

There are four types of expressions in C:

  • Arithmetic expressions

  • Relational expressions

  • Logical expressions

  • Conditional expressions

Each type of expression accepts specific types of operands and uses a specific set of operators. Evaluating a specific expression yields a specific value.

For example:

x = 9/2 + a-b;

The entire above line is a statement, not an expression. The part after the equal sign is the expression.

Detailed Explanation of Expressions in C Language

Arithmetic Expressions

An arithmetic expression is an expression made up of operands and arithmetic operators. Arithmetic expressions compute values of type int, float, or double.

When the expression contains only integer operands, it is called a pure integer expression; when it contains only real number operands, it is called a pure real expression; when it contains both integer and real number operands, it is called a mixed-mode expression.

👇Click to Claim👇
👉C Language Knowledge Resource Collection

Calculation of Arithmetic Expressions

Expressions are calculated by performing one operation at a time. The precedence and associativity of operators determine the order of calculations for individual operations.

When performing a single operation, the following may occur:

  • When both operands are of integer type, the result of the arithmetic operation will be an integer value. For example, 3/2 will yield 1, not 1.5, because the decimal part is ignored.

  • When both operands are of floating-point type, the result of the arithmetic operation will be a real number value. For example, 2.0/2.0 will yield 1.0, not 1.

  • If one operand is of integer type and the other is of real number type, mixed arithmetic operations are performed. In this case, the first operand is converted to a real number operand, and then the arithmetic operation is performed to yield a real number value. For example, 6/2.0 will yield 3.0 because the value of 6 is first converted to 6.0, and then the arithmetic operation is performed to yield 3.0.

Let’s understand this through an example.

6*2/(2+1 * 2/3 + 6) + 8 * (8/4)
Expression Evaluation Description of Each Operation
6*2/( 2+1 * 2/3 +6) +8 * (8/4) Given an expression.
6*2/(2+2/3 + 6) + 8 * (8/4) 2 multiplied by 1 yields 2.
6*2/(2+0+6) + 8 * (8/4) 2 divided by 3 yields 0.
6*2/ 8+ 8 * 2 2 plus 6 yields 8.
6*2/8 + 8 * 2 8 divided by 4 yields 2.
12/8 +8 * 2 8 multiplied by 2 yields 16.
1 + 8 * 2 12 divided by 8 yields 1.
1 + 16 8 multiplied by 2 yields 16.
17 1 plus 16 yields 17.

Relational Expressions

  • Relational expressions are used to compare two operands.

  • They are conditions used to determine whether a certain operation should be taken.

  • In relational expressions, numeric values cannot be compared to string values.

  • The result of a relational expression can be zero or non-zero. Here, a zero value is equivalent to false, and a non-zero value is equivalent to true.

Relational Expressions Description
x%2 == 0 This condition checks if x is even. The relational expression yields 1 when x is even, otherwise it yields 0.
a != b Checks if a is not equal to b. The relational expression yields 1 when a is not equal to b, otherwise it yields 0.
a+b == x+y Checks if the expression “a+b” is equal to the expression “x+y”.
a >= 9 Checks if the value of a is greater than or equal to 9.

Let’s look at a simple example:

#include <stdio.h>
int main(){  int age = 25;  char status;  status = (age>22) ? 'M': 'U';  if(status == 'M')      printf("Married");  else      printf("Unmarried");  return 0;}

Output

Detailed Explanation of Expressions in C Language

Logical Expressions

  • Logical expressions compute a value of 0 or non-zero.

  • They are expressions used for complex conditional testing.

Let’s look at some examples of logical expressions.

Logical Expressions Description
(x > 4) && (x < 6) This is a test condition that checks if x is greater than 4 and x is less than 6. The test condition yields true only when both conditions are true.
x > 10 || y < 11 This is a test condition that checks if x is greater than 10 or y is less than 11. If either condition is true, the test condition yields true.
! (x > 10) && (y == 2) This is a test condition that checks if x is not greater than 10 and y is equal to 2. The test condition yields true only when both conditions are true.

Let’s look at a simple program using the “&&” operator.

#include <stdio.h>
int main(){  int x = 4;  int y = 10;  if ((x<10) && (y>5))  {      printf("Condition is true");  }  else      printf("Condition is false");  return 0;}

Output

Detailed Explanation of Expressions in C Language

Let’s look at a simple example using the “||” operator

#include <stdio.h>
int main(){  int x = 4;  int y = 9;  if ((x < 6) || (y > 10))  {      printf("Condition is true");  }  else      printf("Condition is false");  return 0;}

Output

Detailed Explanation of Expressions in C Language

Conditional Expressions

  • A conditional expression is one that returns 1 when the condition is true, otherwise returns 0.

  • The conditional operator is also known as the ternary operator.

Syntax of Conditional Operator

Assuming exp1, exp2, and exp3 are three expressions.

exp1 ? exp2 : exp3

The above expression is a conditional expression that is evaluated based on the value of exp1. If the condition of expression exp1 is true, the final conditional expression is represented by exp2, otherwise by exp3.

Let’s understand this through a simple example.

#include <stdio.h>
#include <string.h>
int main(){  int age = 25;  char status;  status = (age>22) ? 'M': 'U';  if(status == 'M')      printf("Married");  else      printf("Unmarried");  return 0;}

Output

Detailed Explanation of Expressions in C Language

Detailed Explanation of Expressions in C Language


Popular Recommendations
  • C Language Tutorial – Detailed Explanation of #if in C Language

  • C Language Tutorial – Detailed Explanation of #else in C Language

  • C Language Algorithm – “Concatenating All Words’ Substring” Algorithm Problem

Leave a Comment