CS50 Week 1

Jul 1, 2024

CS50 Week 1 Lecture Notes

Introduction to C

  • Notable shift from Scratch (used in week 0) to C.
  • C is older and more traditional programming language.
  • Key concepts from Scratch: functions, loops, and conditionals.
  • Goals: Problem-solving skills and building a toolkit.
  • Later: understand limitations of computers.

First C Program

  • Transition from Scratch's 'Hello, world' block to C's equivalent:
    #include <stdio.h>
    int main(void) {
        printf("Hello, world\n");
        return 0;
    }
    
  • Key parts:
    • Boilerplate code: #include <stdio.h>, int main(void), {}
    • printf as function equivalent to Scratch's 'say' block.
    • Importance of \n for new line and ; to signal end of statement.

Compilation & Execution

  • Source code: what we write.
  • Machine code: what the computer understands (binary).
  • Compiler: converts source code to machine code (e.g., make command).
  • Running programs: ./program_name (equivalent to clicking an icon).

Tools and Environment

  • Using cs50.dev (cloud-based environment with VS Code).
  • Components of VS Code interface:
    • Editor: writing code.
    • Terminal: typing commands.
    • Explorer: navigating files/folders.
  • CS50 specific tools: cs50.h for simplified functions (e.g., get_string).

Functions with Input and Output

  • Transition from single-output programs to interactive ones:
    #include <cs50.h>
    #include <stdio.h>
    int main(void) {
        string name = get_string("What's your name?\n");
        printf("Hello, %s\n", name);
    }
    
  • Explained the necessity for correct variable types and function signatures.
  • Concept of return values and arguments in functions.

Conditionals in C

  • Simple conditions:
    if (x < y) {
        printf("x is less than y\n");
    }
    
  • Multi-way conditions:
    if (x < y) {
        printf("x is less than y\n");
    } else if (x > y) {
        printf("x is greater than y\n");
    } else {
        printf("x is equal to y\n");
    }
    
  • Importance of correct logical order and nesting to ensure efficiency and correctness.

Variables and Data Types

  • Introduction to different data types (int, float, char, string).
  • Operations on variables:
    int counter = 0;
    counter++;
    counter--;
    
  • Using constants to avoid “magic numbers”:
    const int N = 3;
    

Loops in C

  • While Loop:
    int i = 3;
    while (i > 0) {
        printf("meow\n");
        i--;
    }
    
  • For Loop:
    for (int i = 0; i < 3; i++) {
        printf("meow\n");
    }
    
  • Introduction to do-while Loop:
    int n;
    do {
        n = get_int("Size: ");
    } while (n < 1);
    

Command Line Interface

  • Introduction to basic CLI commands (e.g., ls, mv, rm, mkdir, rmdir).
  • Navigation and file manipulation using CLI.

Error Handling and Design

  • Examples of edge cases and handling improper inputs using loops (e.g., only accepting positive integers).
  • Encouragement to write clear and maintainable code (use of comments, avoiding redundancy).
  • Concepts of scope and variable lifetime.

Limitations in Computing

  • Integer Overflow: when values exceed maximum storage bounds:
    Overflow example: int x = 2147483647; // Max int
    x++; // Results in overflow to -2147483648
    
  • Floating Point Imprecision:
    float y = 1.0 / 3.0;
    printf("%.20f\n", y); // Reveals imprecision in representation
    
  • Real-world problems illustrated with historical bugs (e.g., Y2K, 2038 problem).

Real-world Programming Considerations

  • Examples of integer overflow and precision issues in gaming (Pac-Man, Donkey Kong) and larger systems (Boeing 787).
  • Takeaways: Awareness of hardware limitations, importance of good design, and anticipation of edge cases.

Conclusion

  • Encouragement that making mistakes is part of the learning process.
  • Preview of upcoming topics and problem sets.

Welcome to CS50! 🎉