Introduction
An expression in C is a combination of operators, constants, variables, and function calls that evaluates to a single value. Expressions are the building blocks of C programs and are used to perform calculations, make comparisons, and assign values. Understanding expressions is fundamental to writing effective C programs.
Key Concepts
Expression: Combination of operands and operators that produces a value Operand: Values that operators act upon (variables, constants, function calls) Operator: Symbols that perform operations (+, -, *, /, etc.) Evaluation: Process of computing the final value of an expression
Types of Expressions
Arithmetic Expressions
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 3;
// Simple arithmetic expressions
int sum = a + b; // 15
int product = a * b; // 50
int quotient = a / b; // 2
int remainder = a % c; // 1
// Complex arithmetic expression
int result = a + b * c - a / b; // 10 + 15 - 2 = 23
printf("Sum: %d\n", sum);
printf("Product: %d\n", product);
printf("Complex result: %d\n", result);
return 0;
}
Relational Expressions
#include <stdio.h>
int main() {
int x = 10, y = 20;
// Relational expressions return 1 (true) or 0 (false)
int greater = x > y; // 0
int equal = x == y; // 0
int not_equal = x != y; // 1
int less_equal = x <= y; // 1
printf("x > y: %d\n", greater);
printf("x == y: %d\n", equal);
printf("x != y: %d\n", not_equal);
printf("x <= y: %d\n", less_equal);
return 0;
}
Logical Expressions
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 0;
// Logical expressions
int and_result = (a > 0) && (b > 0); // 1 (both true)
int or_result = (a > 0) || (c > 0); // 1 (at least one true)
int not_result = !(c > 0); // 1 (negation of false)
printf("AND result: %d\n", and_result);
printf("OR result: %d\n", or_result);
printf("NOT result: %d\n", not_result);
return 0;
}
Assignment Expressions
#include <stdio.h>
int main() {
int a, b, c;
// Simple assignment
a = 10; // Expression value is 10
// Multiple assignment
a = b = c = 5; // All variables get value 5
// Assignment with arithmetic
a += 10; // a = a + 10, expression value is new value of a
b *= 2; // b = b * 2
printf("a: %d, b: %d, c: %d\n", a, b, c);
return 0;
}
Operator Precedence and Associativity
Precedence Example
#include <stdio.h>
int main() {
int result;
// Multiplication has higher precedence than addition
result = 2 + 3 * 4; // 2 + 12 = 14 (not 20)
printf("2 + 3 * 4 = %d\n", result);
// Use parentheses to change order
result = (2 + 3) * 4; // 5 * 4 = 20
printf("(2 + 3) * 4 = %d\n", result);
// Complex expression
result = 10 + 5 * 2 - 8 / 4; // 10 + 10 - 2 = 18
printf("10 + 5 * 2 - 8 / 4 = %d\n", result);
return 0;
}
Associativity Example
#include <stdio.h>
int main() {
int a = 2, b = 3, c = 4;
// Left-to-right associativity for same precedence
int result1 = a - b + c; // (2 - 3) + 4 = 3
printf("a - b + c = %d\n", result1);
// Right-to-left for assignment
int x, y, z;
x = y = z = 10; // z = 10, y = z, x = y
printf("x = y = z = 10: x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
Mixed Data Type Expressions
Type Conversion
#include <stdio.h>
int main() {
int i = 10;
float f = 3.14;
double d = 2.718;
// Mixed type expressions
float result1 = i + f; // int promoted to float
double result2 = f + d; // float promoted to double
int result3 = i + (int)f; // explicit cast
printf("int + float: %.2f\n", result1);
printf("float + double: %.3f\n", result2);
printf("int + (int)float: %d\n", result3);
return 0;
}
Complex Expressions
Mathematical Formula
#include <stdio.h>
#include <math.h>
int main() {
double a = 1.0, b = -5.0, c = 6.0;
// Quadratic formula: (-b ± √(b²-4ac)) / 2a
double discriminant = b * b - 4 * a * c;
double root1 = (-b + sqrt(discriminant)) / (2 * a);
double root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots: %.2f and %.2f\n", root1, root2);
return 0;
}
Conditional Expressions
#include <stdio.h>
int main() {
int age = 25, income = 30000;
// Complex conditional expression
int eligible = (age >= 21 && age <= 60) &&
(income >= 25000) &&
(age + income/1000 > 40);
printf("Loan eligible: %s\n", eligible ? "Yes" : "No");
return 0;
}
Expression Evaluation
Side Effects
#include <stdio.h>
int main() {
int i = 5;
// Expression with side effect
int result = i++ + ++i; // Undefined behavior!
printf("Risky expression result: %d\n", result);
// Better approach
i = 5;
int temp1 = i++; // i becomes 6, temp1 = 5
int temp2 = ++i; // i becomes 7, temp2 = 7
result = temp1 + temp2; // 5 + 7 = 12
printf("Safe approach result: %d\n", result);
return 0;
}
Function Calls in Expressions
#include <stdio.h>
int getValue() {
static int count = 0;
return ++count;
}
int main() {
// Function calls in expressions
int result = getValue() + getValue() * 2;
printf("Function call result: %d\n", result);
// Mathematical functions
double x = 3.14159;
double expr = sin(x) + cos(x) + sqrt(x);
printf("Math expression: %.3f\n", expr);
return 0;
}
Practical Examples
Distance Formula
#include <stdio.h>
#include <math.h>
int main() {
double x1 = 1.0, y1 = 2.0;
double x2 = 4.0, y2 = 6.0;
// Distance = √((x2-x1)² + (y2-y1)²)
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
printf("Distance between points: %.2f\n", distance);
return 0;
}
Grade Calculation
#include <stdio.h>
int main() {
int quiz1 = 85, quiz2 = 92, quiz3 = 78;
int midterm = 88, final = 94;
// Weighted average: Quiz 30%, Midterm 30%, Final 40%
double grade = (quiz1 + quiz2 + quiz3) / 3.0 * 0.30 +
midterm * 0.30 +
final * 0.40;
printf("Final grade: %.1f\n", grade);
return 0;
}
Common Mistakes
Operator Precedence Errors
// Wrong: Unexpected precedence
int result = 10 + 5 * 2; // 20, not 30
// Correct: Use parentheses for clarity
int result = (10 + 5) * 2; // 30
Integer Division
// Problem: Integer division loses decimal part
int a = 5, b = 2;
float result = a / b; // 2.0, not 2.5
// Solution: Cast to float
float result = (float)a / b; // 2.5
Best Practices
- Use Parentheses: Make operator precedence explicit
- Avoid Side Effects: Don’t modify variables multiple times in one expression
- Type Awareness: Be conscious of implicit type conversions
- Readability: Break complex expressions into simpler parts
- Consistent Style: Follow coding standards for expression formatting
Summary
Expressions are fundamental components of C programming that combine operators and operands to produce values. Understanding different types of expressions (arithmetic, relational, logical, assignment) and operator precedence is crucial for writing correct programs. Complex expressions should be written with clarity in mind, using parentheses and proper formatting. Be aware of type conversions, side effects, and potential undefined behavior when crafting expressions. Mastering expressions enables you to write efficient and maintainable C code.
Part of BCA Programming with C Course (UGCOA22J201)