Key Programming Concepts and C Language Essentials

Shorthand Assignment Operators

Shorthand assignment operators combine an arithmetic or bitwise operation with assignment. Common examples include:

  • +=: Adds and assigns (x += 5 is equivalent to x = x + 5).
  • -=: Subtracts and assigns (x -= 5 is equivalent to x = x - 5).
  • *=: Multiplies and assigns (x *= 5 is equivalent to x = x * 5).
  • /=: Divides and assigns (x /= 5 is equivalent to x = x / 5).

Call by Value vs. Call by Reference

Call by Value:

  • A copy of the actual argument is passed to the function.
  • Changes made inside the function do not affect the original variable.
  • Used in languages like C (for primitive types).

Call by Reference:

  • The actual argument’s reference (memory address) is passed to the function.
  • Changes made inside the function do affect the original variable.
  • Used in languages like C++ (using pointers) and Python (for mutable objects).

Static Storage Class

The static storage class in programming defines variables that retain their value and state between function calls. It has the following characteristics:

  • Lifetime: The variable exists throughout the program’s execution.
  • Scope: It is limited to the block or function where it is declared.
  • Initialization: It is initialized only once, with a default value of zero if not explicitly initialized.

Typecasting

Typecasting is the process of converting a variable from one data type to another. It allows for explicit control over data conversions in programming.

Types of Typecasting:

  1. Implicit Typecasting (Type Coercion):

    • Performed automatically by the compiler.
    • Example: int x = 5; float y = x; (int to float).
  2. Explicit Typecasting (Manual):

    • Done by the programmer using a cast operator.

Self-Referential Structure

A self-referential structure is a structure in C or similar languages that contains a pointer to an instance of the *same* structure type.

Key Points:

  1. Definition: A structure that includes a member pointing to itself or another instance of the same structure.
  2. Use Case: Commonly used in data structures like linked lists, trees, and graphs.

Memory Allocation: Structures and Arrays

Memory Allocation for Structure:

  1. Non-contiguous: Memory is allocated separately for each member, considering padding and alignment.
  2. Size Consideration: Total size depends on member sizes and compiler-specific padding.

Memory Allocation for Array:

  1. Contiguous: Memory is allocated in a continuous block for all elements of the same type.
  2. Fixed Size: Total size is determined by the number of elements multiplied by the size of each element.

Even/Odd Check with Goto (C Program)

#include <stdio.h>

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (num % 2 == 0)
        goto even;
    else
        goto odd;

even:
    printf("%d is an even number.\n", num);
    return 0;

odd:
    printf("%d is an odd number.\n", num);
    return 0;
}

Callback Functions

  1. Definition: A callback function is a function passed as an argument to another function. This allows the receiving function to execute it at a specific point.
  2. Purpose: Used for event handling, asynchronous operations, and customizing behavior in functions.

Passing Functions as Arguments:

  1. Function Pointer: Use a function pointer to pass a function as an argument.
  2. Declaration: The receiving function must accept a function pointer with the correct signature.
  3. Invocation: The passed function is called within the receiving function using the function pointer.

File Handling in C

Why We Need File Handling:

  1. Data Storage: To store data permanently on storage devices.
  2. Data Retrieval: To read and retrieve data when needed.
  3. Data Processing: To manipulate and update stored data efficiently.

Types of Files:

  1. Text Files: Store data in a human-readable format (e.g., .txt).
  2. Binary Files: Store data in a machine-readable format (e.g., .bin).

Functions in File Operations:

  1. File Opening: fopen() – Opens a file.
  2. File Closing: fclose() – Closes a file.
  3. Reading: fgetc(), fgets(), fread() – Read from a file.
  4. Writing: fputc(), fputs(), fwrite() – Write to a file.
  5. Error Checking: feof(), ferror() – Check file status.