CS50 Week 2: Arrays
On arrays, strings (or are they?) and functions.
Replacing make with clang permalink
- When you run
make hello.c
you get a file calleda.out
(aka assembly output). - To give it a name yourself:
clang -o hello hello.c
- To include custom libraries, you have to include them as a header file, but also tell
clang
where to find it:clang -o hello hello.c -lcs50
(link in the cs50 library)
What is compiling? permalink
Compiling actually involves four steps:
- preprocessing - evaluating libraries and imports, basically looking for everything that has a # symbol. The header files include all the prototypes.
- compiling - translating your code into assembly code
- assembling - translating assembly code into binary
- linking - linking all the different files into one
Debugging permalink
Syntactic errors vs logical errors.
- using
printf
- use
debug50
(a debugger built on the industry standard's tool called GDB - the GBU debugger)- make sure to set up breakpoints before running the debugger!
- rubber duck debugging
Memory permalink
- think of RAM (random-access memory) as a grid, 1 byte taking one space inside the grid.
- RAM is volatile; it requires electricity to continue powering it
- when you run a program in C, it is running in your RAM
- think of the computer's memory as a grid of bytes
// this is inside CS50 IDE
bool 1 byte (bit wasteful, could be just a bit instead)
char 1 byte
double 8 bytes
float 4 bytes
int 4 bytes
long 8 bytes
string ? bytes
Arrays permalink
- An array is a sequence of values stored in memory back to back to back (aka contiguous).
- Arrays are used to hold values of the same data type.
- Arrays can be multidimensional.
- Array values are passed by reference.
With an array declaration, you need to specify three things:
- type
- name
- size
- e.g.
int scores[3];
And this is how you pass an array as an input:
// you use square brackets, but no size
// arrays in C do not know how long they are
// so you'll need to pass in length separately
float average(int length, int array[])
What is a (damn, you) segmentation fault ?
- When you work with data outside of your declared array. For example, you have an array with size 10, but you are accessing the element at position 11.
- Random access is one of the most powerful yet one of the scariest features in C. You can touch, look at, change any memory you want.
🤔 What is the difference between conversion, casting, coercion?
- Conversion: either implicitly or explicitly changing a value from one data type to another, e.g. a 16-bit integer to a 32-bit integer.
- Coercion: an implicit conversion.
- Casting: an explicit type conversion int() or Number()
String permalink
- a string is an array of characters! (there is no 🥄!)
How do we know when the string ends?
\0
null (terminating) character where you basically "waste" one byte to indicate where the string ends.- So a
string
withlength
3, actually takes up 4 bytes. - And how do you loop over the string's characters (for how long)?:
// since every string end is padded with the NULL char
for (int i = 0; str[i] != '\0'; i++) {}
// can be replaced with strlen
// make sure to include the <string.h> header
for (int i = 0, len = strlen(str); i < len; i++) {}
Food for thought 🤔
- The whole point of programming ultimately is to abstract those lower-level implementation details.
Command-line arguments permalink
argc
: argument count. The program's name is by default, the first argumentargv
: argument vector (vector is another word forarray
), is an array of strings. The first elementargv[0]
is always the name of the program. The last element is always stored atargv[argv-1]
- By default,
main
returns 0.
int main(int argc, string argv[])
{
printf("hello, %s\n", argv[1]);
}
else
{
printf("hello, world\n");
}
Exit status permalink
- 0 in computer speak typically means all ok 👍:
return 0
- you can return a custom value (in case of errors or any results you want to keep track of): e.g.
return 1
- to check the exit status of a program you just ran:
echo $?
Functions permalink
- synonyms: procedures, methods, subroutines There are three parts in a function declaration:
- return type
- name
- the argument list (comma-separated, with a type and a name)
🤔 What is the difference between function declaration vs function definition?
float sum_two (int a, int b);
// the definition (aka the actual recipe)
float sum_two (int a, int b)
{
int result = a + b;
return result;
}
- Functions can have 0 (
void
) arguments or no output (alsovoid
).