Operators
Last updated
Last updated
Welcome to the Codes With Pankaj "Operators in C Programming" tutorial! This tutorial will guide you through the various types of operators in C and their usage.
Table of Contents
Operators in C are symbols that perform operations on operands. They are used to manipulate data and perform calculations within expressions.
+
Addition
c = a + b;
-
Subtraction
c = a - b;
*
Multiplication
c = a * b;
/
Division
c = a / b;
%
Modulus (remainder)
c = a % b;
Example:
==
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)
Example:
&&
Logical AND (both conditions true)
if (a && b)
| |
| |
Logical OR (at least one condition true)
!
Logical NOT (reverses the condition)
if (!a)
Example:
=
Simple assignment
c = a + b;
+=
Add and assign
c += 5;
-=
Subtract and assign
c -= 2;
*=
Multiply and assign
c *= 3;
/=
Divide and assign
c /= 2;
%=
Modulus and assign (remainder after division)
c %= 2;
Example:
&
Bitwise AND
z = x & y;
|
|
Bitwise OR
^
Bitwise XOR
z = x ^ y;
~
Bitwise NOT
z = ~x;
<<
Left shift
z = x << 2;
>>
Right shift
z = x >> 1;
Example:
Increment and decrement operators are used to increase or decrease the value of a variable by 1.
Examples:
Increment (++
)
Decrement (--
)
The conditional operator (? :
) is a ternary operator that evaluates a condition and returns one of two values based on the result of the condition.
Example:
Special operators include the sizeof operator, comma operator, address-of operator, and pointer dereference operator.
Examples:
sizeof
operator
Comma operator (,
)
Address-of operator (&
)
Pointer dereference operator (*
)
Operators in C have precedence and associativity rules that determine the order of evaluation in an expression.
Common Mistakes
Confusing assignment (=
) with equality (==
) operator.
Using bitwise operators incorrectly for boolean logic.
Best Practices
Use parentheses to clarify the order of operations in complex expressions.
Understand the precedence and associativity rules to avoid unexpected behavior.
Try these exercises to practice operators in C:
Exercise 1: Write a program to perform arithmetic operations on two numbers.
Exercise 2: Implement a program to compare the ages of two persons using relational operators.
Exercise 3: Create a program to check if a number is even or odd using logical operators.
Exercise 4: Write a program to perform bitwise XOR encryption on a character.
Exercise 5: Implement a program to calculate the factorial of a number using recursion and assignment operators.
We hope this tutorial has helped you understand operators in C programming. Practice with the exercises provided to reinforce your understanding. Happy coding!
For more tutorials, visit .