CS50 Week 1: Introduction to C Programming

Jul 18, 2024

CS50 Week 1: Introduction to C Programming

Overview

Today we delve into C programming, a traditional and older programming language. Although C might look cryptic initially, it builds on concepts from week zero, such as functions, loops, and conditionals. The focus is on learning problem-solving strategies, using C capabilities.

Transition from Scratch to C

  • Last Week: Learned programming basics using Scratch.
  • This Week: Focused on learning C programming language.
  • Key Goal: Equip students with problem-solving tools week by week.

Source Code, Compilation, and Execution

  • Source Code: Code written by humans in a high-level language (C in this case).
  • Machine Code: Binary code (zeros and ones) that the computer executes.
  • Compiler: A program that translates source to machine code.
  • Compilation and Execution Steps:
    1. Writing Code: Using a text editor to write source code files (e.g., hello.c).
    2. Compiling Code: make filename converts source code to an executable.
    3. Running Code: ./filename executes the compiled machine code.

First Program in C: Hello, World!

  • Example:
    #include <stdio.h>
    int main(void) {
        printf("Hello, world!\n");
        return 0;
    }
    
  • Key Components:
    • #include <stdio.h>: Includes standard input-output library.
    • int main(void) { ... }: Main function that returns an integer.
    • printf(...): Function to print formatted output.
    • \n: Newline character.

Advanced Example: Hello with Variables

  • Prompt User for Input:
    #include <cs50.h>
    #include <stdio.h>
    int main(void) {
        string name = get_string("What's your name? ");
        printf("Hello, %s\n", name);
        return 0;
    }
    
  • CS50 Library Functions: get_string to get user input.

Data Types in C

  • Common Types: int, float, char, string (CS50 specific)
  • Examples:
    • Declaring an integer: int x = 5;
    • Declaring a float: float y = 5.5;
    • Declaring a char: char z = 'A';
    • Declaring a string: string s = "hello";

Conditionals in C

  • If-Else 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");
    }
    
  • Key Points: Use of if, else if, else to handle multiple conditions, double equals == for comparison.

Loops in C

  • Patterns: Repeating code multiple times using loops.
  • Example: While Loop:
    int i = 0;
    while (i < 3) {
        printf("meow\n");
        i++;
    }
    
  • For Loop (most common):
    for (int i = 0; i < 3; i++) {
        printf("meow\n");
    }
    

Functions in C

  • Define Custom Function:
    void meow(void) {
        printf("meow\n");
    }
    
    int main(void) {
        for (int i = 0; i < 3; i++) {
            meow();
        }
        return 0;
    }
    
  • Functions with Arguments and Return Values:
    int add(int a, int b) {
        return a + b;
    }
    
    int main(void) {
        int sum = add(2, 3);
        printf("%d\n", sum);  // Outputs 5
        return 0;
    }
    

Command-Line Interface (CLI)

  • Common Commands:
    • ls: List files in directory
    • cd: Change directory
    • cp: Copy files
    • mv: Move (rename) files
    • rm: Remove files
  • Example Usage:
    ls  // Show directory contents
    cd foldername  // Change to directory
    cp source destination  // Copy a file
    mv oldname newname  // Rename a file
    rm filename  // Delete a file
    

Floating Point Imprecision

  • Limitation: Finite bits can't represent all possible decimal numbers.
  • Example:
    float x = 1.0 / 10.0;
    printf("%.10f\n", x);  // Might not perfectly print 0.1000000000
    
  • Precision Issues: Using larger types like double can mitigate this but not eliminate it entirely.

Real-World Applications and Issues

  • Y2K and 2038 Problems: Illustrates how limitations in computing, like integer overflow, can have significant impacts.
  • Notable Bugs:
    • Pac-Man: Level 256 bug due to overflow.
    • Donkey Kong: Level 22 bug due to 8-bit limit.
    • Boeing 787: Power loss after 248 days due to overflow.

Summary

  • C is foundational for understanding low-level operations and managing computer memory precisely.
  • Important concepts include data types, conditionals, loops, functions, and the limitations of memory representation.
  • The transition from graphical (Scratch) to text-based (C) programming helps understand computational thinking deeply.

Next steps include diving into problem sets that necessitate the application of these concepts, gradually building proficiency in solving complex problems programmatically.