Introduction to C Programming Concepts

Aug 20, 2024

CS50 Lecture Notes - Week 1: Introduction to C

Overview

  • Introduction to C, an older and traditional programming language.
  • Concepts from Scratch (week 0) will be revisited: functions, loops, conditionals.
  • Focus on problem-solving and expanding toolkits with C programming.
  • Discuss limitations of computers, specifically what they cannot do well.

Programming in C

Introduction to C

  • Transition from Scratch (a block-based language) to C (a text-based language).
  • Writing source code and compiling it to machine code using a compiler.
  • Introduction to compiling with the make command.

Syntax and Structure

  • Source Code: Human-readable code written by programmers.
  • Machine Code: Binary code (0s and 1s) that the computer executes.
  • Compiler: Translates source code into machine code.

Basic Program - "Hello, World!"

  • First program is to print "Hello, World!" on the screen.
  • Use of printf function in C as an equivalent to Scratch's "say" block.
  • Syntax includes #include <stdio.h>, int main(void), printf("Hello, world\n");

Functions, Variables, and Data Types

  • Functions: printf (output), get_string (input)
  • Variables: Declared with a data type, e.g., string, int.
  • Data Types: int (integer), float (decimal), string (text), char (single character).

Input and Output

  • Getting user input: get_string, get_int from CS50 library.
  • Printing formatted output using printf and format specifiers (%s, %d, %f).

Conditionals and Control Flow

If Statements

  • Syntax for conditional statements: if, else if, else.
  • Use of Boolean expressions for decision making.
  • Comparison operators: <, >, ==.

Logical Operators

  • && (logical AND)
  • || (logical OR)

Scope

  • Variables are only accessible within their defined scope, determined by curly braces {}.

Loops

While Loops

  • Used to repeat actions while a condition is true.
  • Example: Using a counter to repeat actions a specified number of times.

For Loops

  • More concise than while loops, consists of initialization, condition, and increment.
  • Example: for(int i = 0; i < 3; i++) to repeat three times.

Do-while Loops

  • Executes the loop body at least once before checking the condition.

Creating Functions

  • Define custom functions to encapsulate and reuse code.
  • Function definition includes return type, name, parameters, and body.
  • Example: Creating a meow function to print "meow" multiple times.

Limitations of Computers

Integer Overflow

  • Limited by the number of bits used to represent numbers.
  • Example: 32-bit integers can count only up to 4 billion.

Floating Point Imprecision

  • Limits in representing real numbers accurately due to finite memory.
  • Example: 1/3 in float results in rounding errors when extended beyond precision.

Real-World Implications

  • Historical and practical issues with integer overflow in software (Y2K, 2038 problem).
  • Examples from gaming (Pac-Man, Donkey Kong) and real-life systems (Boeing 787).

Conclusion

  • Introduction to C programming with emphasis on learning through practice.
  • Exploration of both the power and limitations of programming and computers.