Today I saw the remaining 2 videos of week 1 at CS50:
- Learned how to use the CS 50 IDE
- On the software world, things take much longer than they seem.
- Important to learn the CMD shortcuts (tab, up and down for history etc. )
- “Computers always use a finite amount of information”
- Awesome thought learned from this video… if you have an 8 bit binary number, and you do modulo 256, you are effectively cutting off the first left 4 bits, and actually any extra bits to the left, because those are all 256*256*256*256 etc… so YourNumber % 256, will remove all those and only leave whatever was before then.
Starting with C
- Main: Analogue on scratch is “When green flag clicked”.
- If wanna write a program, first function has to be main
- Curly braces encapsulates code.
- printf – function who prints a formatted string by formatted means that you can plugin placeholder values, and can specify how many num to print after decimal points etc… and it takes different arguments or parameters… THOSE INPUTS ARE EMBRACED BY PARENTHESIS.
- \n Means New Line. You can’t click enter to create a new line. So we use \n (escaped variable)
- Lastly. the “;” denotes the end of the line. But we don’t put it in all end of lines.
- C can handle different kinds of types:
- Char: Is 8 bits (1 byte), and represents a single character.
- Float: Usually a 32 bit (4 bytes) value, something with a decimal point.
- Double: Twice as big as a float (More bits to store larger numbers)
- Int: Integer Usually 32 bits (4 Bytes) – Means that in unsigned form can store a max num of 4294967295 (Which is 2 to the power of 32 , minus one). And you can basically repesent 4294967295 + 1 values, because you have 0 :).
- Longlong: 64 bits
- Long: Depending on hardware can change!
- String: (Comes only with CS50 library)
- Boolean (Comes only with CS50 library)
- Format Codes: %s is string, %c (print char). %i (print integer), %f (floating or double), etc…
- Escape sequences: \n is new line, \t is tab, \” (a ” when u wanna put it inside an input so that it doesn’t get confused with the ” used to make a string)
- Functions: GetChar, GetFloat etc…
- In C it is a pain in neck like prompt user for input from keyboard so CS50 have generated that library.
- One tricky concept is… how many of a certain number can you store in an X amount of bits. Well , it depends. Do you want to account for negative numbers too? Or only integers? This article explains it well.
- If only integers, you call it “binary unisgned” and you can get the usual (2^n)-1. So for example in 4 bits max num that can be stored is 2^4 minus one, which is 16-1 = 15
- However, usually you wanna do also negative numbers right? In that case you must leave one digit for the +/- sign. Hence in this form, which can be called 2 ways depending on how exactly the negative numbers are “assigned” to each respective binary (check the link above), the max number that can be expressed is 2^(n-1) – 1, because we are missing that 1 bit that we before could use (This is both in 1-s and 2-s complement methods… the only thing that changes in those is the minimum value allowed). This table explains it:
binary unsigned 2s-complement 1s-complement ------------------------------------------- 000 0 0 0 001 1 1 1 010 2 2 2 011 3 3 3 100 4 -4 -3 101 5 -3 -2 110 6 -2 -1 111 7 -1 -0
- On the mario example, the coin score saved is stored in 1 byte (8 bits), hence the maximum coin store that can be saved is 2^8 – 1 = 256-1 = 255.
Some C structures (canonical constructs):