Course Topics
C Basics Introduction and Setup Syntax and Program Structure Comments and Documentation Compiling and Running C Programs Exercise Variables and Data Types Variables and Declaration Data Types (int, float, char, double) Constants and Literals Type Conversion and Casting Exercise Operators Arithmetic Operators Comparison Operators Logical Operators Assignment Operators Bitwise Operators Exercise Input and Output Standard Input/Output (scanf, printf) Format Specifiers File Input/Output Exercise Control Flow - Conditionals If Statements If-Else Statements Switch Statements Nested Conditionals Exercise Control Flow - Loops For Loops While Loops Do-While Loops Loop Control (break, continue) Nested Loops Exercise Functions Defining Functions Function Parameters and Arguments Return Statements Scope and Variables Recursion Exercise Arrays One-Dimensional Arrays Multi-Dimensional Arrays Array Operations Strings as Character Arrays Exercise Pointers Introduction to Pointers Pointer Arithmetic Pointers and Arrays Pointers and Functions Dynamic Memory Allocation Exercise Strings String Handling String Functions (strlen, strcpy, strcmp) String Manipulation Exercise Structures Defining Structures Structure Members Arrays of Structures Pointers to Structures Exercise File Handling Opening and Closing Files Reading from Files Writing to Files File Positioning Exercise Memory Management Static vs Dynamic Memory malloc() and free() Memory Leaks Best Practices Exercise Advanced Topics Preprocessor Directives Macros Header Files Modular Programming Exercise Final Project Project Planning Building Complete Application Code Organization Testing and Debugging Exercise

Arithmetic Operators

Introduction

  • Understanding arithmetic operators is fundamental to programming in C language as they are used to perform mathematical operations on variables and values.
  • In this lesson, you'll learn about different types of arithmetic operators, their examples, and best practices for using them effectively.

Core Concepts

Arithmetic Operators: These operators are used to perform calculations such as addition, subtraction, multiplication, division, and modulus.

  1. Addition (+): Adds two numbers together. Example: result = a + b;
  2. Subtraction (-): Subtracts one number from another. Example: result = a - b;
  3. Multiplication (*): Multiplies two numbers. Example: result = a * b;
  4. Division (/): Divides one number by another. Example: result = a / b;
  5. Modulus (%): Returns the remainder of the division operation. Example: result = a % b;

Practical Examples

#include <stdio.h>
int main() {
    int num1 = 20, num2 = 5;
    float decimalNum = 10.5f;

    // Arithmetic operations examples
    printf("Addition: %d + %d = %d\n", num1, num2, num1 + num2);
    printf("Subtraction: %d - %d = %d\n", num1, num2, num1 - num2);
    printf("Multiplication: %d * %d = %d\n", num1, num2, num1 * num2);
    printf("Division: %d / %d = %.2f\n", num1, num2, (float)num1 / num2);
    printf("Modulus: %d %% %d = %d\n", num1, num2, num1 % num2);

    // Floating-point arithmetic operations example
    float result = decimalNum + 5.7f;
    printf("Addition of floats: %.2f + 5.7 = %.2f\n", decimalNum, result);

    return 0;
}

Common Issues and Solutions (CRITICAL SECTION)

NameError

What causes it: Misspelling an arithmetic operator or variable name.

# Bad code example that triggers the NameError
int res = a + b;  // Misspelled '+' as 'res' instead of '+='

Error message:

Traceback (most recent call last):
    File "example.py", line X, in <module>
        error_code_here
NameError: name '+' is not defined

Solution: Correctly spell the arithmetic operator and variable names.

# Corrected code
int result = a + b;  // Use proper spelling of '+' and 'result'

Why it happens: Spelling errors in code can cause unexpected behavior, resulting in NameErrors.

How to prevent it: Double-check your spelling and use an IDE with built-in spell checkers to help catch these mistakes.

TypeError

What causes it: Attempting arithmetic operations on incompatible data types (e.g., int + float).

# Bad code example that triggers the TypeError
int num1 = 5;
float num2 = 3.5f;
int sum = num1 + num2; // Incorrect addition of incompatible data types

Error message:

Traceback (most recent call last):
    File "example.py", line X, in <module>
        error_code_here
TypeError: can't convert 'float' to 'int' without a parenthetic cast

Solution: Cast the data type to be compatible with arithmetic operations.

# Corrected code
int num1 = 5;
float num2 = 3.5f;
int sum = (int)(num1 + num2); // Parenthetically cast float to int before addition

Why it happens: Incompatible data types can't be directly combined without a typecast.

How to prevent it: Be aware of the data types you are working with and use proper type casting when needed.

Best Practices

  • Use parentheses to make complex expressions clearer and easier to understand.
  • Utilize variables for storing intermediate results, making your code more readable.
  • Keep in mind that integer division will truncate the decimal part (e.g., 7 / 3 = 2).

Key Takeaways

  • C language has five arithmetic operators: addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
  • Proper spelling, data type compatibility, and understanding the behavior of each operator are crucial for writing effective and error-free code.
  • Always use parentheses and variables to improve readability and maintainability of your C programs.

Next steps for learning: Learn about comparison operators, logical operators, and bitwise operators in C language.