In C, an expression is a combination of operators, constants, variables, and function calls that can be evaluated to produce a value. Expressions are used to perform calculations, manipulate data, and make decisions in a C program.
Types of Expressions in C
1. Arithmetic Expressions:
These expressions involve mathematical operations like addition, subtraction, multiplication, and division.
Example
Description
x + y
Addition
x - y
Subtraction
x * y
Multiplication
x / y
Division
x % y
Modulus (remainder)
2. Relational Expressions:
These expressions are used for comparing values and return a Boolean result (true or false).
Example
Description
x == y
Equal to
x != y
Not equal to
x < y
Less than
x > y
Greater than
x <= y
Less than or equal to
x >= y
Greater than or equal to
3. Logical Expressions:
These expressions involve logical operators and return a Boolean result.
Example
Description
x && y
Logical AND
`x
!x
Logical NOT
4. Bitwise Expressions:
These expressions perform bitwise operations on integers.
Example
Description
x & y
Bitwise AND
`x
y`
x ^ y
Bitwise XOR (exclusive OR)
~x
Bitwise NOT (complement)
x << n
Left shift by n bits
x >> n
Right shift by n bits
5. Assignment Expressions:
These expressions assign a value to a variable.
Example
Description
x = y
Assign y to x
x += y
Increment x by y
x -= y
Decrement x by y
x *= y
Multiply x by y
x /= y
Divide x by y
x %= y
Assign the remainder of x/y
6. Conditional (Ternary) Expressions:
These expressions provide a way to make decisions based on a condition.
Example
Description
x ? y : z
If x is true, return y; else return z
7. Comma Expressions:
These expressions evaluate multiple expressions separated by commas and return the value of the last expression.
Example
Description
x, y, z
Evaluate x, then y, then z and return z
8. Function Call Expressions:
These expressions involve calling a function and can return a value.