Operators
C Programming Tutorial: Operators
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
1. Introduction to Operators
Operators in C are symbols that perform operations on operands. They are used to manipulate data and perform calculations within expressions.
2. Arithmetic Operators
Operator  Description  Example 

 Addition 

 Subtraction 

 Multiplication 

 Division 

 Modulus (remainder) 

Example:
3. Relational Operators
Operator  Description  Example 

 Equal to 

 Not equal to 

 Greater than 

 Less than 

 Greater than or equal to 

 Less than or equal to 

Example:
4. Logical Operators
Operator  Description  Example 

 Logical AND (both conditions true) 

      Logical OR (at least one condition true) 
 Logical NOT (reverses the condition) 

Example:
5. Assignment Operators
Operator  Description  Example 

 Simple assignment 

 Add and assign 

 Subtract and assign 

 Multiply and assign 

 Divide and assign 

 Modulus and assign (remainder after division) 

Example:
6. Bitwise Operators
Operator  Description  Example 

 Bitwise AND 

    Bitwise OR 
 Bitwise XOR 

 Bitwise NOT 

 Left shift 

 Right shift 

Example:
7. Increment and Decrement Operators
Increment and decrement operators are used to increase or decrease the value of a variable by 1.
Examples:
Increment (
++
)Decrement (

)
8. Conditional Operator (Ternary Operator)
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:
9. Special Operators
Special operators include the sizeof operator, comma operator, addressof operator, and pointer dereference operator.
Examples:
sizeof
operatorComma operator (
,
)Addressof operator (
&
)Pointer dereference operator (
*
)
10. Operator Precedence and Associativity
Operators in C have precedence and associativity rules that determine the order of evaluation in an expression.
11. Common Mistakes and Best Practices
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.
12. Exercises
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 www.codeswithpankaj.com.
Last updated