C Arithmetic Operators šŸŽÆ

beginner
20 min

C Arithmetic Operators šŸŽÆ

Welcome to our in-depth guide on C Arithmetic Operators! We'll be exploring the world of arithmetic operations in C, learning how to perform basic and complex calculations, and understanding the role they play in real-world projects. Let's dive in!

Understanding Arithmetic Operators šŸ“

Arithmetic operators in C are symbols used to perform mathematical operations. We'll be covering 5 main types of operators:

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Division (/)
  5. Modulus (% - Remainder)

Basic Arithmetic Operations šŸ’”

Let's get familiar with these operators using simple examples:

c
#include <stdio.h> int main() { int a = 10, b = 20; printf("Addition: %d\n", a + b); // 30 printf("Subtraction: %d\n", a - b); // -10 printf("Multiplication: %d\n", a * b); // 200 printf("Division: %.2f\n", (float)a / b); // 0.50 printf("Modulus: %d\n", a % b); // 0 (Since 10 is not divisible by 20 with a remainder) return 0; }

šŸ’” Pro Tip: Always cast the operands to the same data type for division to avoid unexpected results.

Arithmetic Operator Precedence šŸ“

Arithmetic operators follow a specific order of operations, also known as precedence. C uses the following order from highest to lowest:

  1. Postfix Operators
  2. Unary Operators
  3. Multiplicative Operators (*, /, %)
  4. Additive Operators (+, -)
  5. Shift Operators (<<, >>)
  6. Relational Operators (<, >, <=, >=)
  7. Equality Operators (==, !=)
  8. Bitwise AND (&)
  9. Bitwise XOR (^)
  10. Bitwise OR (|)
  11. Logical AND (&&)
  12. Logical OR (||)
  13. Conditional Operator (?:)
  14. Assignment Operators (=, +=, -=, *=, /=, %=)
  15. Comma Operator (,)

This order is crucial when writing complex expressions to ensure the correct results are calculated.

Quiz Time šŸŽÆ

Let's test your understanding of arithmetic operators with a quick quiz:

Quick Quiz
Question 1 of 1

What will be the output of the following code?

Stay tuned for more C tutorials on CodeYourCraft! Happy learning! šŸŒšŸŽ‰