Lecture 1 - Introduction, Software Setup, Hello World¶
0. Introduction¶
- Welcome to CMSC 14300 / CS143 - Systems Programming
- What is "systems programming"?
- Getting closer to the machine than Python lets you
- Bits, bytes, pointers, memory - making the computer's operations transparent
- Why C?
- The lingua franca of programmers; foundational, historic, still everywhere
- Quirky and flawed, but one of the great practical language designs
- You already program (Python, data structures) - now we go a level down
- Course arc (Weeks 1-8): | Wk | Topics | |----|--------| | 1 | Basics, the terminal, control structures, functions, loops | | 2 | Operators, bits, binary encoding | | 3 | Pointers, allocation, arrays | | 4 | Strings, structs, unions | | 5 | Linked lists, hash tables | | 6 | pthreads, locks | | 7 | Trees | | 8 | Sorting, graphs |
1. Syllabus & Logistics¶
- Walk through the course website live (
site/) and Ed - Instructor: Suhail Rehman - JCL158, office hours by appointment
- Meetings: Mon & Wed, 6:00-8:30 PM, RY277
- Textbook: K&R, The C Programming Language - recommended, not required
- Grading: 50 / 50
- 50% programming assignments
- 50% quizzes + final exam
- Guarantees: 92 -> A-, 82 -> B-, 72 -> C-, 65 -> D- (boundaries may drop, never rise)
- Big quiz/assignment discrepancies -> handled as special cases
- Final exam: in class, Wed Aug 5 (last lecture)
- Late policy: 3 late days total (Gradescope, per-minute); then 4% of total course grade per day; no assignment > 3 days late
- Quizzes: weekly, ~20-30 min, start Week 3 (Mondays); cover prior week
- Ungraded PSets: released Wednesdays, done before next class, discussed in class, used to prep for quizzes
- Academic integrity: don't copy / don't be copied; document collaboration; cite sources (even small ones, as code comments)
- Generative AI policy - read carefully:
- No LLMs to write CS143 code
- No pasting your CS143 code into an LLM for suggestions/fixes
- No uploading course material (syllabus, assignments) to an LLM
- Make sure your work is actually your work
2. Computer Systems Overview¶
- A few Basics - Input, Processing, Output
- Hardware vs. Software
- Hardware: the physical components of a computer (CPU, memory, storage)
-
Software: the programs and instructions that run on the hardware
-
Von Neumann Architecture
- The stored-program concept: instructions and data are stored in the same memory
- CPU fetches instructions, decodes them, and executes them
-
This architecture is the foundation of most modern computers
-
Low-level vs High-Level Programming
- Low-level: closer to the machine, involves direct hardware manipulation
- High-level: abstracts away hardware details, easier to write and understand
- This distinction is not absolute; think of it as a spectrum
- The absolute lowest level is machine code and/or assembly language, which is the pure representation of the computer's instructions that run on a CPU - these are in Binary (more on binary in the next few lectures). Assembly language is a machine code that has been made human-readable using mnemonics.
- Python is a high-level programming language, which abstracts away many low-level details.
-
C is a lower-level programming language, which provides more direct control over hardware and memory.
-
Compiled vs. interpreted
- Python: interpreted, dynamic typing, runs via an interpreter
- Python code is converted to bytecode (an intermediate representation)
- C: compiled ahead of time to native machine code
- The pipeline: source code -> compiler -> machine code -> execution
- What the machine actually has (teaser, depth comes later):
- CPU executes instructions; memory holds code + data
- Stack vs. heap distinction -> revisited Week 3 (pointers/allocation)
- Static typing in C
- Types are mandatory; the program must type-check to compile
- Types guide memory layout and catch errors before the program runs
3. Intro to the C Language¶
- History/lineage: Kernighan & Ritchie, Bell Labs, Unix - the "K&R" book
- Our toolchain:
clangas the compiler - Anatomy of a minimal C program:
#include <stdio.h>- bring in declarations (here,printf)int main(void)- the entry point; returns anintstatus- statements end with
; return 0;- 0 means success- The edit -> compile -> run loop
Live-coding break - Hello World¶
- Write
hello.ctogether, compile withclang, run it - Full walkthrough in the handout:
hello_world.md - Deliberately break it (drop a
;, drop the#include) -> read the errors
4. C Programming Basics - Syntax¶
- Coming from Python: the biggest visible changes
- Code blocks are delimited by braces
{ }, not by indentation - Statements must end with a semicolon
; - Whitespace/indentation is for humans - the compiler ignores it (so indent anyway, for readability)
- Comments:
// single lineand/* block comment */ - Everything lives inside functions; execution starts at
main - A program is a sequence of declarations and statements; order matters
- Common beginner errors (preview them now, you'll meet them again):
- missing
;, mismatched{ }, calling something before it's declared
5. Data Types & Variables¶
- Static typing: every variable has a fixed type, declared before use
int count;thencount = 5;- or together:int count = 5;- Contrast with Python, where a name can be rebound to any type
- Core built-in types (sizes are typical, not guaranteed - more in Week 2):
| Type | Holds | Example literal |
|------|-------|-----------------|
|
int| whole numbers |42,-7| |char| a single byte / character |'A','\n'| |float| single-precision real |3.14f| |double| double-precision real |3.14159| charis really a small integer -'A'is65(teaser for binary/ASCII week)- Integer division truncates:
7 / 2is3, not3.5(a classic bug) - Mix in a
doubleto get real division:7 / 2.0is3.5 - Variables are uninitialized garbage until you assign them - unlike Python
6. Control Structures¶
- Conditionals:
if (cond) { ... } else if (cond) { ... } else { ... }- Conditions are integers:
0is false, anything non-zero is true - Loops:
while (cond) { ... }for (init; cond; update) { ... }- e.g.for (int i = 0; i < n; i++)do { ... } while (cond);- runs the body at least onceswitch/case/break/default- multi-way branch on an integer- Jump statements:
break(leave a loop),continue(next iteration) - Watch out:
if (x = 5)assigns instead of comparing - use==for equality
7. printf & Format Specifiers¶
printfprints formatted text; the first argument is a format string- Format specifiers are placeholders filled by the remaining arguments:
| Specifier | Prints |
|-----------|--------|
|
%d|int| |%f|float/double| |%c| single character | |%s| string (Week 4) | |%%| a literal%| - Examples:
printf("count = %d\n", count);printf("%d C = %f F\n", c, f);\nis a newline; without it, output runs together- The specifier must match the argument's type -
%dwith adoubleis undefined behavior (no error, just wrong output) - a recurring theme in C
Reading input with scanf¶
scanfis the mirror image ofprintf: it reads formatted input from the user instead of writing it- Same specifiers (
%d,%f,%c, ...), but there's one crucial difference: - You must pass the address of the variable, using
& scanf("%d", &n);- read anintintonscanf("%lf", &temp);- read adouble(note:%lf, not%f, forscanf)- The
&("address-of") givesscanfsomewhere to store the result; without it you'll get garbage or a crash. Why this is needed becomes clear once we reach pointers (Week 3) - for now, just remember:scanfneeds& - Always prompt first so the user knows you're waiting:
In-class exercise break¶
1. Celsius -> Fahrenheit. Read a Celsius value and print the Fahrenheit
equivalent. Discuss why we write 9.0 / 5.0 (real division) and not 9 / 5
(which truncates to 1).
#include <stdio.h>
int main(void) {
double celsius;
printf("Enter temperature in Celsius: ");
scanf("%lf", &celsius);
double fahrenheit = celsius * 9.0 / 5.0 + 32.0;
printf("%.1f C = %.1f F\n", celsius, fahrenheit);
return 0;
}
- Point out
%.1f- a format modifier that prints one digit after the decimal.
2. The Count's Counting Program. In the spirit of Count von Count from
Sesame Street: read a number n from the user, then count from 1 up to n,
one per line, and finish with a triumphant total. A simple for loop over a
couple of int variables.
#include <stdio.h>
int main(void) {
int n;
printf("How high shall we count today? ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("%d! Ah-ah-ah!\n", i);
}
printf("%d wonderful numbers! Mwa-ha-ha-ha!\n", n);
return 0;
}
- Discuss the loop anatomy:
init; cond; update, and what happens whennis0or negative (the loop body never runs - a good edge case to mention).
8. Software Setup Overview¶
- Need a Linux or macOS machine (Windows: WSL)
- Tools we install this quarter (all free / open-source, industry-standard):
clang- compilergit- version controllldb- debuggervalgrind- memory analysis- Detailed setup is self-serve this week - follow the posted instructions; bring problems to Ed / office hours
9. IDEs, Editors & Command-Line Tools - opinions¶
- Editor vs. IDE - what's the difference, and does it matter now?
- Why we start at the terminal + compiler directly:
- You learn what the IDE is doing for you
- The skills transfer to any machine / any future job
- Quick survey: VS Code, vim/neovim, emacs, nano
- Pick one you can be productive in; don't fight your tools
- If you haven't learned a text editor yet, I am partial to vim.