Introduction
An expression is a combination of operands and operators that evaluates to a single value. Expressions are one of the fundamental building blocks of C programs and are used in assignments, calculations, function calls, conditions, and many other programming tasks.
Every time you perform a calculation, compare two values, or call a function that returns a value, you are using an expression.
What Is an Expression?
An expression consists of one or more operands connected by one or more operators. When evaluated, an expression produces a single result.
Syntax
operand operator operand
For example, in the expression 5 + 3, the numbers 5 and 3 are operands, while + is the operator.
Simple Expressions
A simple expression contains one operator and one or more operands.
Example
10 + 5
20 - 8
6 * 7
18 / 3
25 % 4
Each expression evaluates to a single value.
Operands
Operands are the values on which operators perform operations. Operands may be:
- Constants
- Variables
- Function return values
- Array elements
- Pointer dereferences
Example
int a = 5;
int b = 10;
a + b
In this example, a and b are operands.
Arithmetic Expressions
Arithmetic expressions perform mathematical calculations.
Example
int total = (10 + 5) * 2;
int remainder = 17 % 3;
Arithmetic expressions use operators such as +, -, *, /, and %.
Relational Expressions
Relational expressions compare two values and produce either true or false. In C, these results are represented as non-zero (true) or zero (false).
Example
age >= 18
score == 100
x != y
price < limit
These expressions are commonly used in if, while, and for statements.
Logical Expressions
Logical expressions combine one or more relational expressions using logical operators.
Example
(age >= 18) && (age <= 65)
(score >= 50) || (bonus > 0)
!finished
Logical expressions are useful when multiple conditions must be evaluated together.
Assignment Expressions
Assignment expressions store a value in a variable. The assignment operator itself is part of the expression.
Example
x = 10;
total = price * quantity;
count += 5;
The value of an assignment expression is the value assigned to the variable.
Increment and Decrement Expressions
The increment (++) and decrement (--) operators increase or decrease a variable by one.
Example
i++;
++i;
j--;
--j;
These operators can be used in both prefix and postfix forms, which differ in when the increment or decrement takes effect.
Function Call Expressions
A function call is also an expression if the function returns a value.
Example
int length = strlen(text);
double root = sqrt(number);
The returned value can be assigned to a variable or used within a larger expression.
Expression Evaluation
The compiler evaluates an expression according to operator precedence and associativity.
Example
int result = 5 + 2 * 3;
Multiplication has higher precedence than addition, so the expression is evaluated as:
Example
5 + (2 * 3)
The final value of result is 11.
Expressions vs Statements
Expressions and statements are closely related but are not the same.
| Expression | Statement |
|---|---|
| Produces a value. | Performs an action. |
| Can be part of a statement. | May contain one or more expressions. |
a + b |
result = a + b; |
In the statement result = a + b;, the expression is a + b, while the complete assignment is the statement.
Complete Example
The following program demonstrates several different kinds of expressions.
Example
#include <stdio.h>
int main(void)
{
int a = 10;
int b = 5;
int sum = a + b;
int product = a * b;
if (sum > product)
{
printf("Sum is larger.\n");
}
else
{
printf("Product is larger or equal.\n");
}
return 0;
}
This program uses:
- Arithmetic expressions
- Assignment expressions
- Relational expressions
- Logical evaluation inside an
ifstatement - Function call expressions
Common Beginner Mistakes
- Confusing the assignment operator (
=) with the equality operator (==). - Ignoring operator precedence.
- Writing expressions with unnecessary complexity.
- Using variables before they are initialized.
- Forgetting that integer division discards the fractional part.
Summary
Expressions are combinations of operands and operators that evaluate to a single value. They are used throughout C programs for calculations, comparisons, assignments, and function calls.
The main types of expressions include:
- Arithmetic expressions
- Relational expressions
- Logical expressions
- Assignment expressions
- Increment and decrement expressions
- Function call expressions
Understanding expressions is essential because they appear in almost every C program and form the foundation for writing efficient and correct code.