Operators and Expressions in C
By Swann

- Published on

Sharing
Introduction
Operators in C programming are symbols that instruct the compiler to perform specific mathematical or logical manipulations. Expressions are combinations of variables, constants, and operators that are evaluated to yield a result. Together, operators and expressions form the backbone of computations and logic in C programs.
Types of Operators in C
Arithmetic Operators
- Addition (
+):sum = a + b; - Subtraction (
-):difference = a - b; - Multiplication (
*):product = a * b; - Division (
/):quotient = a / b; - Modulus (
%):remainder = a % b;
Relational Operators
- Equal to (
==):if(a == b) - Not equal to (
!=):if(a != b) - Greater than (
>):if(a > b) - Less than (
<):if(a < b) - Greater than or equal to (
>=):if(a >= b) - Less than or equal to (
<=):if(a <= b)
Logical Operators
- Logical AND (
&&):if(a > 0 && b > 0) - Logical OR (
||):if(a > 0 || b > 0) - Logical NOT (
!):if(!a)
Bitwise Operators
- AND (
&):c = a & b; - OR (
|):c = a | b; - XOR (
^):c = a ^ b; - NOT (
~):c = ~a; - Left shift (
<<):c = a << 2; - Right shift (
>>):c = a >> 2;
Expressions in C
Expressions involve variables, constants, and operator combinations, evaluated to produce a value.
- Arithmetic Expression:
total = price * quantity; - Relational Expression:
is_equal = (a == b); - Logical Expression:
is_valid = (a > b) && (c < d);
Usage in C Programs
#include<stdio.h>
int main() {
int a = 5, b = 10, sum, product;
int is_equal, is_greater;
// Arithmetic Operators
sum = a + b;
product = a * b;
// Relational Operators
is_equal = (a == b);
is_greater = (a > b);
printf("Sum: %d, Product: %d\n", sum, product);
printf("Is Equal: %d, Is Greater: %d\n", is_equal, is_greater);
return 0;
}
Conclusion
Understanding and implementing operators and expressions in C is pivotal for computations, decision-making, and controlling the program flow. As you delve deeper into C programming, mastering operators and creating expressions will allow you to develop robust algorithms and solve complex computational problems.