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
+
Addition
c = a + b;
-
Subtraction
c = a - b;
*
Multiplication
c = a * b;
/
Division
c = a / b;
%
Modulus (remainder)
c = a % b;
Example:
3. 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)
Example:
4. Logical Operators
&&
Logical AND (both conditions true)
if (a && b)
| |
| |
Logical OR (at least one condition true)
!
Logical NOT (reverses the condition)
if (!a)
Example:
5. Assignment Operators
=
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:
6. Bitwise Operators
&
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:
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, address-of operator, and pointer dereference operator.
Examples:
sizeof
operatorComma operator (
,
)Address-of 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