Saturday Jan. 2 to Friday, Jan. 8.
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++.
make
. The command Malan types is make hello
and make
presumes that if you want to make hello that there is a file called hello.c
. Make produces hello
, and then Malan executes that by doing ./hello
at the prompt.get_string
. Declaring a variable and assigning a return value to it, e.g., string answer = get_string("What's your name? ");
. Formatting of string output using printf
and %s
. At this point, you should stop the video and get everything in this screenshot to work in the IDE.int main(void)
boilerplate. Introducing header files, starting with stdio.h
. Introducing compiler error output, and help50
, a command provided by the cs50 staff that premasticates compiler error output for newbies. Introducing code comments and style50
to help you write stylistically good quality code. Introducing check50
which will do much of the grading of your problem solutions automatically. Showing what the binary file hello
looks like in the IDE.ls
, rm
, mv
, mkdir
, cd
, and ..
for the parent directory.string
: char
, float
, double
, int
, and long
. Introducing more CS50 conveniences analogous to get_string
: get_char
, get_float
, get_double
, get_int
, and get_long
. Introducing printf specifiers analogous to %s
: %c
, %f
, %lf
, %i
, %li
. Introducing operators for division, remainder, etc. Example: addition.c
. Illustrating truncation in integer division: truncation.c
. Using a cast to fix the truncation bug.while
and for
statements. Declaring and defining a function. Function arguments. Implementing get_positive_int
using the do-while statement and returning a value from a function. Variable scope.mario.c
which printed out various number of rows and columns of bricks. Another example imprecision.c
that illustrates the limitations of floating point arithmetic, and finally an example overflow.c
that illustrates the limitation of integer arithmetic. The 2038 problem (analogous to the Y2K problem).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 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:
My credit.c
output for a few inputs: