Operators and Expressions
In C programming, operators are special symbols that perform operations on variables and values. Here’s an overview of the main categories of operators in C:
1. Arithmetic Operators
These operators perform basic mathematical operations:
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)%
(Modulus)
2. Relational Operators
These operators compare two values and return a boolean result (true
or false
):
==
(Equal to)!=
(Not equal to)>
(Greater than)<
(Less than)>=
(Greater than or equal to)<=
(Less than or equal to)
3. Logical Operators
These operators are used to combine or negate boolean expressions:
&&
(Logical AND)||
(Logical OR)!
(Logical NOT)
4. Bitwise Operators
These operators perform operations on bits and are useful for low-level programming:
&
(Bitwise AND)|
(Bitwise OR)^
(Bitwise XOR)~
(Bitwise NOT)<<
(Left shift)>>
(Right shift)
5. Assignment Operators
These operators assign values to variables:
=
(Simple assignment)+=
(Add and assign)-=
(Subtract and assign)*=
(Multiply and assign)/=
(Divide and assign)%=
(Modulus and assign)&=
,|=
,^=
,<<=
,>>=
(Bitwise assignments)
6. Increment and Decrement Operators
These are shorthand for adding or subtracting 1 from a variable:
++
(Increment)--
(Decrement)
7. Conditional (Ternary) Operator
This operator is a shorthand for an if-else
statement:
condition ? expr1 : expr2;
If the condition is true, expr1
is executed; otherwise, expr2
is executed.
8. Comma Operator
This operator allows you to evaluate two expressions and returns the value of the second:
a = (b = 3, b + 2); // a gets the value 5
9. Sizeof Operator
This operator returns the size (in bytes) of a data type or variable:
sizeof(int); // Typically returns 4 on many systems
10. Pointer Operators
These operators are used with pointers:
*
(Dereference operator)&
(Address-of operator)
Example
Here’s a simple example using some of these operators:
#include <stdio.h>
int main() {
int a = 5, b = 10;
// Arithmetic
int sum = a + b; // sum = 15
// Relational
if (a < b) {
printf(“a is less than b\n”);
}
// Logical
if (a && b) {
printf(“Both a and b are non-zero\n”);
}
// Increment
a++;
// Sizeof
printf(“Size of int: %zu bytes\n”, sizeof(int));
return 0;
}
Expression
In C, an expression is a combination of variables, constants, operators, and function calls that are evaluated to produce a value. Expressions are fundamental to programming in C as they form the building blocks for calculations, assignments, and control flow. Here’s a breakdown of different types of expressions in C:
1. Arithmetic Expressions
These use arithmetic operators to perform calculations.
int a = 5, b = 10;
int sum = a + b; // sum = 15
2. Relational Expressions
These compare two values and yield a boolean result (1
for true, 0
for false).
if (a < b) {
// true block
}
3. Logical Expressions
These use logical operators to combine boolean values.
if (a > 0 && b > 0) {
// both a and b are positive
}
4. Bitwise Expressions
These perform bitwise operations on integer types.
int c = a & b; // Bitwise AND
5. Assignment Expressions
These assign a value to a variable.
a = 10; // Assigns 10 to a
6. Compound Assignment Expressions
These combine an arithmetic operation with an assignment.
7. Increment and Decrement Expressions
These modify a variable’s value by 1.
a++; // Increment a by 1
b–; // Decrement b by 1
8. Conditional (Ternary) Expressions
These provide a shorthand way to express if-else logic.
int max = (a > b) ? a : b; // max gets the larger of a or b
9. Function Call Expressions
These invoke functions and can return values.
int length = strlen(“Hello”); // Calls strlen function
10. Comma Expressions
These evaluate multiple expressions and return the last one.
int result = (a = 5, b = 10); // result gets the value 10
11. Sizeof Expressions
This returns the size of a variable or data type.
size_t size = sizeof(int); // size holds the size of an integer
Example of Mixed Expressions
Here’s a complete example showcasing various types of expressions:
#include <stdio.h>
#include <string.h>
int main() {
int a = 5, b = 10;
// Arithmetic expression
int sum = a + b; // sum = 15
// Relational expression
if (a < b) {
printf(“a is less than b\n”);
}
// Logical expression
if (a > 0 && b > 0) {
printf(“Both a and b are positive\n”);
}
// Conditional expression
int max = (a > b) ? a : b;
printf(“Max: %d\n”, max); // Output: Max: 10
// Function call expression
int length = strlen(“Hello”);
printf(“Length: %d\n”, length); // Output: Length: 5
// Comma expression
int result = (a = 5, b = 10); // result = 10
printf(“Result: %d\n”, result);
return 0;
}