This tutorial will help you understand expressions in C. You’ll learn what they are, how they work, and why they matter. We’ll look at real examples and explain rules like which part runs first. By the end, you’ll know how to use expressions in your code.
What is an Expression in C?
An expression in C is a valid combination of operands and operators that computes a value.
For example:
a + b
Here, a and b are operands, and + is the operator. The result of the expression is the sum of a and b.
Key Components of C Expressions
Expressions are made up of:
- Operands: Variables, constants, or literals (e.g., a, 42, 3.14)
- Operators: Symbols that perform operations (e.g., +, -, *, /, %, =)
- Function Calls: Functions returning values (e.g., sqrt(x))
Types of Expressions in C
C expressions can be classified into the following categories:
- Arithmetic Expressions
- Relational Expressions
- Logical Expressions
- Conditional (Ternary) Expressions
- Increment and Decrement Expressions
Arithmetic Expressions in C
These expressions perform arithmetic operations on both literals and variables. Some of the commonly used arithmetic operators are + (addition), – (subtraction), * (multiplication), / (division) and % (modulus).
Arithmetic Operators in C
Operator | Symbol | Operation |
---|---|---|
Addition | + | Adds two operands |
Subtraction | – | Subtracts right operand from left |
Multiplication | * | Multiplies two operands |
Division | / | Divides left operand by right |
Modulus | % | Gives remainder after division (integers only) |
Syntax of Arithmetic Expressions
result = operand1 operator operand2;
Examples of Arithmetic Expressions
int a = 8, b = 3; int x; x = a + b; // x = 11 x = a - b; // x = 5 x = a * b; // x = 24 x = a / b; // x = 2 (integer division) x = a % b; // x = 2 (remainder of 8/3)
Relational Expressions in C
These expressions compare two values or variables and return either true or false based on the outcome of the comparison. The commonly used relational operators are == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to) and <= (less than or equal to).
For example, the expression 5>6 will return false as 5 is not greater than 6 while the expression 6>=6 will return true as 6 is greater than or equal to 6.
#include <stdio.h> int main() { int a = 5, b = 6; if(a>b) { printf("true"); }printf("false"); }
Output:
false
Logical Expressions in C
Logical expressions in C are used to make decisions based on true or false values. They are typically used in conditional statements, loops, and control flow mechanisms. These expressions evaluate to either 1 (true) or 0 (false).
Syntax of Logical Expressions
expression1 operator expression2
Or for NOT:
!expression
Examples of Logical Expressions
1. Logical AND (&&)
Returns true only if both conditions are true.
int a = 10, b = 5; if (a > 5 && b < 10) { // True, because both conditions are true }
2. Logical OR (||)
Returns true if at least one condition is true.
if (a == 10 || b == 0) { // True, because a == 10 is true }
3. Logical NOT (!)
Reverses the truth value.
if (!(a < 5)) { // True, because a < 5 is false, so !false = true }
4. Combining Logical and Relational Expressions
Logical expressions often involve relational expressions like >, <, ==, etc.
if ((x >= 10 && x <= 20) || y == 0) { // Do something if x is in [10,20] OR y is 0 }
Conditional Expressions in C
The conditional expression is a operator that takes three operands. The first operand is a boolean expression, and the second and third operands are values. Conditional operator, also known as the ternary operator.
Syntax:
expression1 ? expression2 : expression3;
Example:
#include <stdio.h> void main() { int a = 8; int b = 7; int c = a < b ? a = b : b++; printf("%d", c); }
Output:
7
Increment and Decrement Expressions
In C programming, increment and decrement expressions are used to increase or decrease the value of a variable by one. These are unary operators, meaning they operate on a single operand.
Example:
int a = 3; int b = a++ + 2; // b = 5, a = 4 (a is used first, then incremented) int c = ++a + 2; // a = 5, then c = 7 (a is incremented first)
Operator Precedence
In C, operator precedence defines the order in which operators are evaluated in an expression. Operators with higher precedence are executed before those with lower precedence.
For example:
int result = 10 + 5 * 2; // result = 10 + (5 * 2) = 20
Here, * has higher precedence than +, so multiplication occurs first.
If operators have the same precedence, associativity determines the direction of evaluation:
- Most operators are left-to-right
- Assignment and unary operators are right-to-left
Use parentheses to clearly define the order and improve readability:
int result = (10 + 5) * 2; // result = 30
Understanding precedence helps you avoid logic errors in complex expressions.
FAQs on Expressions in C
1. What is an expression in C?
An expression in C is a valid combination of variables, constants, operators, or function calls that produces a value.
2. How is an expression different from a statement?
An expression evaluates to a value, while a statement performs an action. For example, assigning a value or calling a function is a statement.
3. What is operator precedence and why is it important?
Operator precedence defines the order in which operators are evaluated in an expression. It ensures that some operations are performed before others, unless parentheses are used to change the order.
4. What is associativity in expressions?
Associativity determines the direction in which operators of the same precedence are evaluated. Most operators are left-to-right associative, while a few like assignment are right-to-left.
5. Can expressions be combined?
Yes, different types of expressions can be combined to form more complex expressions, such as combining arithmetic with logical or relational expressions.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Check C Books
- Apply for Computer Science Internship
- Check Computer Science Books
- Apply for C Internship
- Practice BCA MCQs