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

Example:

int a = 10, b = 3;
int sum = a + b;    // sum = 13
int diff = a - b;   // diff = 7
int product = a * b; // product = 30
int quotient = a / b; // quotient = 3
int remainder = a % b; // remainder = 1

3. Relational Operators

Example:

int x = 5, y = 10;
int isEqual = (x == y);  // isEqual = 0 (false)
int isGreater = (x > y); // isGreater = 0 (false)

4. Logical Operators

Example:

int isTrue = 1, isFalse = 0;
int result1 = (isTrue && isFalse); // result1 = 0 (false)
int result2 = (isTrue || isFalse); // result2 = 1 (true)
int result3 = !isTrue;           // result3 = 0 (false)

5. Assignment Operators

Example:

int x = 10;
x += 5;  // x = 15
x -= 3;  // x = 12
x *= 2;  // x = 24 
x /= 4;  // x = 6
x %= 3;  // x = 0

6. Bitwise Operators

Example:

unsigned int x = 5;   // 0101 in binary
unsigned int y = 9;   // 1001 in binary
unsigned int result = x & y;  // result = 1 (0001)

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:

int result = (x > y) ? x : y;

9. Special Operators

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 (*)

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

  1. Confusing assignment (=) with equality (==) operator.

  2. Using bitwise operators incorrectly for boolean logic.

Best Practices

  1. Use parentheses to clarify the order of operations in complex expressions.

  2. Understand the precedence and associativity rules to avoid unexpected behavior.

12. Exercises

Try these exercises to practice operators in C:

  1. Exercise 1: Write a program to perform arithmetic operations on two numbers.

  2. Exercise 2: Implement a program to compare the ages of two persons using relational operators.

  3. Exercise 3: Create a program to check if a number is even or odd using logical operators.

  4. Exercise 4: Write a program to perform bitwise XOR encryption on a character.

  5. 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