Week 1 Notes — C

Saturday Jan. 2 to Friday, Jan. 8.

Week 1 Video (duration 2:16:48)

Stop the video after each example and type in what Malan has typed in. Don’t even think of skirting this by finding somewhere to copy and paste the code from. Part of the learning process is typing in the simple examples yourself — correctly — you will be surprised to discover myriad ways to make mistakes even in simple examples. Stylistically, you should type in what Malan has written character for character. When he puts a curly brace on a line by itself (a convention that is going overboard on whitespace, IMHO, but you should nonetheless follow for now), or indents a line of code by four spaces (by hitting the tab key), or puts in one space around each side of an equals sign, you should too. The C compiler does not care about any of the aforementioned whitespace. It is for readability.

If you would like a more structured text to complement Malan’s introduction to C, the standard pedagogical and authoritative reference for C is Kernighan and Ritchie’s The C Programming Language, 2nd Edition. Dennis Ritchie authored C. It is a successor to a language called B. ANSI C became standard in 1989. Many things you could get away with in the original C are discouraged in ANSI C, at the cost of some verbosity. The 2nd Edition of Kernighan and Ritchie is for ANSI C. A language called C++ was grafted onto C and is pretty popular, despite its many failings. When you are learning C, you should ignore the existence of C++.

Around the 55-minute point in this lecture, some shell commands were introduced. If you are hungering for a more systematic introduction to shell commands, try reading the first several sections of the Bash Reference Manual.

By the way, there are many shells. zsh is arguably the best and most popular shell nowadays, but there are many people that would vociferously choose bash instead. To understand why, you’d have to read The GNU Manifesto. The CS50 IDE is based on Ubuntu Linux, and its standard shell is bash. The common ancestor of both zsh and bash is the original sh, and it is really lacking in features. Having briefly looked at quite a few introductions to bash, I can recommend Introduction to the Bash Command Line as a pedestrian place to start. The Bash Reference Manual would be your next stop, but only if you want to become a power user of bash.

Problem Set 1

Problem Set 1 is to write two programs. One is an elaboration of mario.c. The other is to write a program credit.c to test the type and validity of credit card numbers. More specific directions are at the link above.

My mario.c output for a height of 15:

mario.c output

My credit.c output for a few inputs:

credit.c output