Exercises
- Exercises - exercises to go with the labs and lectures
Reference
- Your computer’s filesystem
- IDEs, Code editors, and using SFTP to edit remote files locally
- Multi-file development
Examples
To get the examples on your own computer, right-click and save to download them.
To get the examples on thoth:
- Right-click on the example link, and click “Copy link” or equivalent
- On thoth, make sure you are in the directory that you want to put the example
- On thoth, type
wget, a space, then paste the link, and hit enter
thoth will download the file from this site directly.
Safari users: Safari sometimes changes file extensions. Do not trust it.
| Intro and C Basics | |
|---|---|
| 1_hello_world.c | Welcome to C! |
| 1_cant_add_strings.c | You're not in Java-land anymore. |
| Animation of 1_cant_add_strings | Step through with "Next >". Watch str = str + x especially. |
| 1_variable_weirdness.c | "Fun" things can happen when you don't initialize variables. |
| 1_printf.c | Printing things out with C's version of System.out.println. |
| 1_fgets.c | Getting input with fgets... kinda. |
| Functions | |
| 2_get_line_fail.c | Coming up with the signature for a function that takes an array parameter. |
| 2_get_line_success.c | The proper way to write the get_line function. |
| 2_get_line_prototype.c | Using a function prototype to access a function before it's defined. |
| 2_return_array_fail.c | Returning local arrays is a bad idea. |
| Pointers | |
| 3_sizeof.c | sizeof() might not work how you expect. |
| 3_pointers.c | Shows the address-of &x and value-at *p operators to create and access pointers. |
| 3_arrays_are_pointers.c | Yep. Shows array address calculation. |
| 3_array_of_strings.c | Double-pointers are usually used for arrays-of-arrays. Like arrays of strings! |
| 3_2d_arrays.c | 2D arrays and arrays-of-arrays are actually different things! |
| 6_const_ptrs.c | The const keyword is a neat addition to pointers. |
| 6_ptr_casts.c | Pointer casting lets you "reinterpret" the meaning of bit patterns. |
| Animation of 6_ptr_casts | (Click "Visualize Execution") You can step through it line-by-line and see what's happening. |
| 6_ars.c | Shows addresses of local variables inside ARs as functions are called. |
| Strings | |
| 4_strings_are_bad.c | Strings in C are bad, okay? |
| 4_cant_modify_literals.c | String literals are different beasts. |
| 4_str_size_length.c | strlen() and sizeof() are very different things. |
| Files | |
| 4_read_write_text_file.c | Reading/writing text files is pretty straightforward. |
| 5_binary_file.c | Reading/writing binary files is also pretty straightforward. |
| The heap (malloc/free) | |
| Linked list | (Click "Visualize Execution") Very basic linked list on the heap. |
| 7_memory_leak.c | Use valgrind to see the memory this program leaks. |
| Use-after-free | (Click "Visualize Execution") after freeing heap memory, it's invalid. |
| Structs, Enums, Typedefs | |
| 5_food_struct.c | How to define and use a simple struct. |
| 5_linked_list.c | Making self-referential struct pointers. |
| 5_enum_color.c | Common patterns for using enums. |
| Misc | |
| 5_cmd_args.c | Command-line arguments, like you'll need for proj1! |
| 10_a_terrible_function.java | Here's exactly how NOT to write a function. Thanks, MARS. |
| 10_a_better_function.java | Pseudocode improvement of the previous function. |
| 10_whats_wrong.c | A program that crashes. Use the debugger to figure out why! |
| 14_asm_test.c | Stuff to look at in gdb. |
| Multi-file compilation and make | |
| 11_compile.sh | A very simple compilation script. You'll have to chmod +x it to use it. |
| Makefile | The makefile for the following example. IMPORTANT: make sure its name is Makefile exactly. |
| 11_main.c | The main file. |
| 11_user.c | The second file. |
| 11_user.h | The header file for 11_user.c. |
| Function Pointers | |
| 12_qsort.c | Using qsort. |
| 12_qsort_structs.c | Another qsort example, but sorting structs this time. |
| ArraySorting.java | That same example, done with equivalent Java features. |
| 12_method.c | Function pointers make Java-style method calls work. |
| Linking and Loading | |
| 11_main_island.c | Goes with the next file. |
| 11_sub_island.c | Goes with the previous file. |
| 12_innocent.c | Goes with the next file. |
| 12_bad.c | Goes with the previous file. |
| Processes | |
| 13_pmap.c | Prints its own (virtual) memory map. |
| 13_mmap.c | Uses mmap() to allocate some memory and to "load" a file. |
| 16_fork.c | Demonstrates the fork() POSIX API function. |
| 16_exec.c | Demonstrates the execvp() POSIX API function. |
| 16_waitpid.c | Seeing how a child process terminated. |
| 16_signal.c | Setting up a signal handler for... nefarious purposes... |