Week 1: Introduction to C Programming

Jul 15, 2024

Week 1: Introduction to C Programming

Overview

  • Instructor: David Malan
  • Topic: Introduction to C, building on basics from Scratch.

Key Concepts

Transition from Scratch to C

  • Last week: Scratch - visual programming (functions, loops, conditionals)
  • This week: C programming language - traditional syntax
  • Purpose: Learn problem-solving tools for programming.

Importance of C

  • Focuses on problem-solving with functions, variables, conditionals, loops, etc.
  • Highlights limitations of computers in terms of solving problems and representing information.

Source Code and Machine Code

  • Source Code: Code written by humans (high-level language)
  • Machine Code: Binary code that computers understand (low-level language)
  • Conversion by Compilers: Translates source code to machine code.

Programming Environment

  • Using VS Code in the cloud (https://cs50.dev)
  • Tabbed interface with text editor, terminal, and file explorer.
  • Command-line Interface (CLI): Execute commands using the keyboard (e.g., compiling, running programs).
  • Graphical-user Interface (GUI): Menus, icons, and interactions through mouse clicks.

Writing Simple C Programs

Hello, World in C

  • Translate Scratch’s “Say” block to C:
    #include <stdio.h>
    
    int main(void) {
        printf("Hello, world\n");
    }
    
  • Importance of #include <stdio.h>, int main(void), printf, and \n.

Variables in C

  • Declaration and initialization: int counter = 3;
  • Modifying Variables:
    • counter = counter + 1;
    • counter += 1;
    • counter++;

Conditionals in C

  • Example of an if statement:
    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");
    }
    
  • Comparison of Scratch and C syntax

Loops in C

  • While Loop: Repeat until a condition is false
    int counter = 3;
    while (counter > 0) {
        printf("meow\n");
        counter--;
    }
    
  • For Loop: More compact syntax
    for (int i = 0; i < 3; i++) {
        printf("meow\n");
    }
    
  • Infinite loop example using while (true)

User Defined Functions in C

  • Creating custom functions:
    void meow(int n) {
        for (int i = 0; i < n; i++) {
            printf("meow\n");
        }
    }
    
    int main(void) {
        meow(3);
    }
    
  • Function Prototypes: Declaring functions before use.

Common Functions in C

  • CS50 Library: Simplifies getting user input (#include <cs50.h>)
    • Example functions: get_string(), get_int(), get_float(), get_char()

CLI Overview

  • Common Commands: ls, cd, mkdir, mv, rm, cp
  • File Management: Navigating directories, creating/removing directories/files, renaming and copying files.

Programming Challenges and Limits

Integer Overflow

  • Definition: Exceeding the maximum value for an integer type causes it to wrap around.
  • Example: Counting beyond the capacity of 32-bit integers (~4 billion).

Floating Point Imprecision

  • Definition: Limited precision in representing real numbers due to fixed number of bits.
  • Example: 1/3 not accurately represented beyond a certain number of decimal places.

Real-World Implications

  • Historical bugs: Y2K bug, Boeing 787 software bug.

Summary

  • Emphasized foundational programming concepts in C.
  • Introduction to limitations of computing (integer overflow, floating point imprecision).
  • Prepared for future discussions and problem sets by covering core syntax and logic in C.