Expressions are one of the most important parts of the C language.
Every calculation, comparison, assignment, or function call in C is done using expressions.
An expression is a combination of:
- Variables
- Constants
- Operators
- Function calls
that produces a single value when evaluated.
What Is an Expression?
An expression is a valid combination of operators and operands that the C compiler evaluates to produce a value.
Example:
5 + 3
This expression evaluates to 8.
Another example:
a + b * c
Types of Expressions in C
C supports several types of expressions based on their usage and output. Each type helps perform different operations.
1. Arithmetic Expressions
Used for mathematical operations such as +, -, *, /, %.
Example:
int a = 10, b = 3;
int result = a + b * 2; // result = 16
Order of evaluation follows precedence rules:* and / have higher precedence than + and -.
2. Relational Expressions
These expressions compare two values and return TRUE (1) or FALSE (0).
Operators:
> < >= <= == !=
Example:
int x = 10, y = 20;
printf("%d", x < y); // Output: 1 (true)
3. Logical Expressions
Used to combine relational expressions.
Operators:
&& || !
Examples:
(x > 5) && (y < 10)
(x == 10) || (z != 0)
!(a == b)
4. Assignment Expressions
Used to assign values to variables.
Basic Assignment:
x = 5;
Compound Assignment:
x += 5; // x = x + 5;
x -= 2; // x = x - 2;
x *= 3; // x = x * 3;
x /= 4; // x = x / 4;
x %= 2; // x = x % 2;
5. Increment and Decrement Expressions
These operators change the value by 1.
Examples:
i++; // post-increment
++i; // pre-increment
i--; // post-decrement
--i; // pre-decrement
Difference:
int a = 5;
printf("%d", a++); // Output: 5, then a becomes 6
printf("%d", ++a); // a becomes 7, then Output: 7
6. Conditional Expression (Ternary Operator)
This is a short form of an if-else statement.
Syntax:
condition ? expression1 : expression2
Example:
int age = 18;
char *result = (age >= 18) ? "Adult" : "Minor";
7. Constant Expressions
Expressions using only constants.
Example:
int a = 5 * 10 + 2; // constant expression
8. String Expressions
Used with string functions.
Example:
printf("%s", "Hello " "World");
Output:
Hello World
9. Pointer Expressions
Used when working with pointers.
Example:
int a = 10;
int *p = &a;
printf("%d", *p); // Output: 10
10. Function Call Expressions
A function call is itself an expression because it returns a value.
Example:
int result = printf("Hello");
printf() returns the number of characters printed.
Precedence and Associativity in Expressions
When multiple operators appear in an expression, C uses precedence rules:
Highest โ Lowest
()(Parentheses)++,--*,/,%+,-- Relational operators
- Logical operators
- Assignment operators
Example:
int x = 10 + 20 * 2; // evaluates as 10 + (40) = 50
Example Program Using Different Expression Types
#include <stdio.h>
int main() {
int a = 10, b = 20;
int sum = a + b; // Arithmetic
int isGreater = (a < b); // Relational
int logic = (a < b) && (b > 5); // Logical
int max = (a > b) ? a : b; // Ternary
printf("Sum = %d\n", sum);
printf("a < b = %d\n", isGreater);
printf("Logical Result = %d\n", logic);
printf("Max = %d\n", max);
return 0;
}
Conclusion
Expressions are the backbone of C programming.
Every calculation, comparison, assignment, or logic in your program depends on expressions.
Understanding how expressions work helps you write efficient and error-free C code.
๐ View other articles about C Programming:
https://savanka.com/category/learn/c-programming