Lesson 7
Operators and Expressions
Operators are special symbols used to perform operations on variables and values in C. Expressions combine operators, variables, and constants to produce a single result.
C Track
Save progress as you learn.
44 lessons
Lesson content
Operators and Expressions in C Programming: Types and Examples
Operators are special symbols used to perform operations on variables, constants, and values in a C program. An expression is a combination of operators, variables, constants, and function calls that produces a single result. Understanding operators and expressions is essential for writing efficient and error-free C programs.
What Are Operators and Expressions in C?
- Operator: A symbol that tells the compiler to perform a specific operation on one or more operands.
- Operand: A variable or value on which an operator acts.
- Expression: A combination of variables, constants, operators, and function calls that evaluates to a single value.
- Operator Precedence: The rules that determine the order in which operators are evaluated in an expression.
C provides several categories of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Increment and Decrement Operators
- Bitwise Operators
- Conditional (Ternary) Operator
- Special Operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
- + (Addition) - a + b
- - (Subtraction) - a - b
- * (Multiplication) - a * b
- / (Division) - a / b
- % (Modulus / Remainder) - a % b
Example
#include <stdio.h> // Includes standard input/output library
int main() // Main function
{
int a = 20, b = 6; // Declares and initializes variables
printf("Addition = %d\n", a + b); // Adds two numbers
printf("Subtraction = %d\n", a - b); // Subtracts two numbers
printf("Multiplication = %d\n", a * b); // Multiplies two numbers
printf("Division = %d\n", a / b); // Performs integer division
printf("Remainder = %d\n", a % b); // Finds the remainder
return 0; // Ends the program
}Relational Operators
Relational operators compare two values and return either true (1) or false (0).
- == (Equal to)
- != (Not equal to)
- > (Greater than)
- < (Less than)
- >= (Greater than or equal to)
- <= (Less than or equal to)
Example
a > bIf a is greater than b, the result is 1; otherwise, it is 0.
- Relational Operator: An operator that compares two values and returns true (1) or false (0).
Logical Operators
Logical operators combine multiple conditions.
- && (Logical AND) - True only if both conditions are true
- || (Logical OR) - True if at least one condition is true
- ! (Logical NOT) - Reverses the result of a condition
Example
(age >= 18 && age <= 60)The expression is true only if both conditions are true.
Assignment Operators
Assignment operators assign values to variables. Compound assignment operators simplify expressions and make code shorter.
- = (Assign value) - a = b
- += (Add and assign) - a += b is equivalent to a = a + b
- -= (Subtract and assign) - a -= b is equivalent to a = a - b
- *= (Multiply and assign) - a *= b is equivalent to a = a * b
- /= (Divide and assign) - a /= b is equivalent to a = a / b
- %= (Modulus and assign) - a %= b is equivalent to a = a % b
Increment and Decrement Operators
These operators increase or decrease a variable's value by one.
- ++ (Increment) - Increases value by 1
- -- (Decrement) - Decreases value by 1
Pre-increment
++aThe value is increased before it is used.
Post-increment
a++The current value is used first, then increased. The same concept applies to the decrement operator (--).
- Pre-increment (++a): Value is incremented before use.
- Post-increment (a++): Value is used first, then incremented.
Bitwise Operators
Bitwise operators work directly on the binary representation of integers. They are commonly used in embedded systems, device drivers, network programming, and performance optimization.
- & (Bitwise AND)
- | (Bitwise OR)
- ^ (Bitwise XOR)
- ~ (Bitwise NOT)
- << (Left Shift)
- >> (Right Shift)
Conditional (Ternary) Operator
The conditional operator is a shorthand form of the if...else statement.
Syntax
condition ? expression1 : expression2;Example
largest = (a > b) ? a : b;If a is greater than b, largest receives the value of a; otherwise, it receives the value of b.
- Ternary Operator: A shorthand conditional operator (?:) that selects one of two expressions based on a condition.
Special Operators
C provides several special-purpose operators.
- sizeof - Returns the size of a data type or variable
- & (Address-of operator) - Returns the memory address of a variable
- * (Pointer dereference operator) - Accesses the value at a pointer's address
- , (Comma operator) - Evaluates multiple expressions sequentially
- . (Structure member access) - Accesses a member of a structure
- -> (Structure pointer member access) - Accesses a member via a structure pointer
Example
sizeof(int)Returns the number of bytes occupied by the int data type.
Operator Precedence and Associativity
When an expression contains multiple operators, C follows operator precedence to determine the order of evaluation.
10 + 5 * 2Multiplication is performed first, giving 10 + 10 = 20. Parentheses can be used to change the order of evaluation.
(10 + 5) * 2With parentheses, addition is performed first, giving 15 * 2 = 30. Using parentheses improves readability and avoids unexpected results.
Best Practices
- Use parentheses to make complex expressions easier to understand.
- Avoid writing overly complicated expressions.
- Choose compound assignment operators (+=, -=, etc.) when appropriate.
- Be careful when using pre-increment and post-increment operators.
- Always consider operator precedence while writing expressions.
- Avoid using assignment (=) instead of equality (==) in conditions.
- Avoid multiple increment/decrement operators in a single expression.
- Avoid complex nested ternary operators.
FAQ About Operators and Expressions in C
1. What is an operator in C programming?
An operator is a symbol that performs a specific operation on one or more operands, such as addition, comparison, or assignment.
2. What is an expression in C?
An expression is a combination of variables, constants, operators, and function calls that evaluates to a single value.
3. What is the difference between = and == in C?
The = operator assigns a value to a variable, while the == operator compares two values to determine whether they are equal.
4. What are arithmetic operators in C?
Arithmetic operators perform mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
5. What is the conditional (ternary) operator?
The conditional operator (?:) is a shorthand form of the if...else statement that selects one of two expressions based on a condition.
6. Why is operator precedence important?
Operator precedence determines the order in which operators are evaluated in an expression. Understanding precedence helps prevent logical errors and ensures expressions produce the expected results.