When I first started writing part 2, I thought I could cover the entire lecture from week 0. Turns out there was a lot to cover.
Also I was writing this CS50 updated the course from the 2017 version to the 2018 version, which meant the videos are slightly different. But that’s okay, i’m just going to cover what I can to give you a gist of everything.
So here is a quick overview of week 1:
Conditionals
A condition is a logical structure that will run a block of code based on an expression being true or false. It is like being on a road with a fork that goes one of two or more ways.
Note: 0 and 1 can also be interpreted as false(0) and true(1). So in the last conditional statement the first if is skipped because it is false, while the else if evaluates to true and runs the code within that block.
Compiled Languages
A compiled language is a programming language written in source code, which we the programmers write. Afterwards a separate program called a compiler will transform that code into machine readable code and an executable file the programmer can run on the machine. When programming in C we use the Clang compiler.
CS50 IDE
CS50 includes a website where you can run an IDE (Integrated development Environment) remotely. This makes every students IDE system identical, which makes it easier for the CS50 staff to test and evaluate on their end.
CLI
Within the IDE is also a CLI (command line interface) where you can manipulate the operating system using only text. Instead of clicking on folders or right clicking to do other operations, everything is done using short text commands in the CLI. I find “pwd”, “cd” + wherever I want to go, “ls”, and “ls -a” to be my most used commands right now. In a previous job I “google hacked” my way through a linux script which is basically a long terminal command.
CS50 IDE Debugger
If you ever need to figure out what is going on in your code line by line, and see what your variable mutations are etc, use the CS50 debugging tool. To use this tool, within the CS50 IDE where your source code is showing, click just left of the line number until you see a red dot like this:
Then use the CLI to find the correct location of your program and instead of running it the usual way, for example, ./myprogram, type debug50 followed by the name of the program, in this case debug50 myprogram.
Here is what happens after running that command on my credit program:
On the right you can see a list of variables and their current value, using the controls above that list I can go iteration by iteration viewing the values step by step. This is super useful when trying to figure out if things are what you expect them to be or not!
simple debugging tip
A common issue when starting out in C is remember to compile the source code you write to an executable. The executable is the actual program, and it is a separate file. So you might have a program you wrote:
- you make a change in the source code (example.c)
- save your source code
- run the program
Nothing changes because you didn’t compile. So remember to:
- you make a change in the source code (example.c)
- save your source code
- compile the source code using gcc or clang
- run the program
Show a programs output to the terminal window
In C there are functions included in libraries known as header files. The printf() is a function included in the stdio.h header file (stdio is short for standard IO library). You include a library using the #include directive followed by the library you want to use #include <stdio.h>. Once you have this statement you have access to functions like printf().
And the output:
I use printf using two arguments, a string containing the information I want displayed, and the variables I want to be put in place of the % format placeholders. Each data type has it’s own % formatter. You can see in the code above when I want to replace the % formatter with an integer, I use %d. There are also special characters to express a tab or newline. There are \t and \n respectively.
Getting user input
The CS50 IDE has it’s own library. Using cs50.h we have access to the functions get_string() and get_int().
Something that really bugged me was trying to figure out how to get the cs50 library to work. I found through a google search the github repository which lead me to some documentation. When you run the clang command in the terminal you need the -lcs50 flag to link the library when compiling (or linking…I think). So if you get something that says it can’t find a definition for the function get_string(), just add that flag to clang.
EDIT: Turns out there is an even better way using the “make” command. If you have a file called test.c for example just type “make test” without the “.c” part and it will make the executable test with all the flags required. There are these “shorts” videos after the main lecture that go over it.
Real numbers vs Integers
Something to be careful of is truncation during integer operations. If you run something like:
int num1 = 5; int num2 = 2; printf("%d\n", num1/num2);
you’re going to get 2, because it just takes that .5 and chops it right off. Doesn’t matter what is after that point, it just cuts it off and keeps what is on the left. So if you want the correct answer use the float data type which takes a real number with a fractional part.
Data types
int – Whole number
float – A real number (has a fractional part)
double – A double precision real number (More decimal places of precision)
char – A single character
void – A special character that is valueless (I have no idea how this works)
The sizeof() Operator
This is not a function included in any header, it’s actually a unary operator that will return the number of bytes of whatever variable you give it as an argument.
Output: Size of num1 in bytes: 4
Memory Bounds
Not going to pretend I understand how this works. So disclaimer i’m babbling here about what I think this means. In the lecture David talks about a lego game and Ghandi in the game civilization becoming extremely violent. Because data types are not infinite, surpassing the limit of a data type causes numbers, for example to sometimes wrap back around themselves, giving you zero or even a negative number depending on the data type.
So that is the week 1 lecture. Also I am not going to try to have all the answers on everything I am learning. Having another screen on my right with the info and putting it into my own words is not really going to help me and probably not anyone reading this hoping to actually know what a noob goes through during this time. I can say writing these little pieces of code in the IDE and putting them up on gist has really helped me understand how to write C with less errors and i’m looking forward to challenging myself with the problem sets that are coming up.
Until next time.