C - Expressions

In C, expressions are combinations of operators, variables, constants, and function calls that produce a single value. Expressions can be simple or complex, and they are the fundamental building blocks for performing calculations and making decisions in a program.

Here are some key points about expressions in C:

  1. Types of Expressions:
    • 'Primary Expressions': These are the simplest form of expressions and include constants, variables, and function calls. For example, 5, x, and func() are primary expressions.
    • 'Arithmetic Expressions': These involve arithmetic operators like '+', '-', '*', '/', and '%'. For example, x + 3 is an arithmetic expression.
    • 'Relational Expressions': These involve relational operators like '==', '!=', '<', '>', '<=', and '>='. For example, x == 10 is a relational expression.
    • 'Logical Expressions': These involve logical operators like '&&', '||', and '!'. For example, x > 0 && y < 5 is a logical expression.
    • 'Assignment Expressions': These involve assignment operators like '=', '+=', '-='. For example, x = 42 is an assignment expression.
  2. 'Operator Precedence': Operators in expressions have precedence levels, which determine the order of evaluation. For example, multiplication ('*') has higher precedence than addition ('+'), so 3 + 5 * 2 is evaluated as 3 + (5 * 2), resulting in 13.
  3. 'Operator Associativity': When operators have the same precedence, associativity determines the order of evaluation. For example, addition and subtraction both have left-to-right associativity, so 3 - 2 + 1 is evaluated as (3 - 2) + 1, resulting in 2.
  4. 'Parentheses': You can use parentheses to override the default precedence and control the order of evaluation. For example, (3 + 5) * 2 ensures that addition is performed before multiplication.
  5. 'Side Effects': Some expressions have side effects, meaning they modify variables or perform actions when evaluated. For example, x++ increments the value of x.
  6. 'Function Calls': Function calls are also expressions. When a function is called, its return value is the result of the expression. For example, result = calculate(); assigns the return value of the calculate function to result.
  7. 'Type Conversion': C performs automatic type conversion when necessary to ensure that operands of an operator are compatible. For example, when adding an int and a double, the int is automatically converted to a double before addition.
  8. 'Complex Expressions': You can create complex expressions by combining multiple operators and operands. For example, if (x > 0 && (y < 5 || z == 10)) is a complex expression that combines relational and logical operators.

Here's a simple example that demonstrates various types of expressions:


#include <stdio.h>

int main() {
    int x = 5;
    double y = 3.0;

    // Arithmetic expression
    double result1 = x + y;

    // Relational expression
    int result2 = x > 3;

    // Logical expression
    int result3 = (x < 10) && (y > 2.0);

    printf("Result1: %lf\n", result1);
    printf("Result2: %d\n", result2);
    printf("Result3: %d\n", result3);

    return 0;
}

This example demonstrates arithmetic, relational, and logical expressions, along with primary expressions. The results are printed to the console.