**C Programming Exercise** CS 105 You will complete this exercise in class, and you will not need to submit anything to gradescope. You will work in groups of two or three with only one student coding at a time. We might not all complete the exercise during class, but I am glad to help out during office hours if you want to continue working after class. You all will work fairly independently in your groups, but I will occasionally demonstrate each exercise on the projector. # Logging into the server If you are **not** on Pomona's WiFi or Ethernet, you will first need to connect to the Pomona VPN. If you do not already have the Pomona VPN set up on your machine, there are [instructions for how to set-up the VPN here](https://servicedesk.pomona.edu/support/solutions/articles/18000021757). Once you are on Pomona's network, you should `ssh` into the course VM using the following command and your Pomona CAS username and password ~~~bash ssh USERNAME@itbdcv-lnx04p.campus.pomona.edu ~~~ Here is [a nice reference for command line commands](https://cs.pomona.edu/classes/cs105/assignments/commandline.pdf). # Writing your first C program You can code on the server using a command line text editor (like `vim`, `emacs`, `nano`, or `micro`), or using Visual Studio Code and the [Remote - SSH](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh) extension. Let's write the hello world program for C: 1. Create a directory in which to store your code (e.g., in `~/cs105/exercises/01CProgramming/`). 2. Edit a new file called `hello.c`. 3. Write code for that prints `"Hello, World!"`. Use [C reference](https://en.cppreference.com/w/c) as a reference. # Running your first C program Now that we have the program, we need to compile it and run in. 1. Compile your program with the following command: ~~~c gcc hello.c ~~~ 2. Run your program with the following command: ~~~bash ./a.out ~~~ # Local and global variables I recommend duplicating your file and deleteing/adding code for each exercise from here forward. Now, - add some variables to the global scope and to the main function, and - print the value and memory addresses of these variables. What do you notice about the memory addresses? Guess the segment in which these variables exist (stack, heap, data, bss, or code; see [the drawing from Monday](https://cs.pomona.edu/classes/cs105/drawings/MonAug29-ComputerSystems.pdf)). # Pointers Now, - create a pointer-type variable that points to one of your local variables, and - print the value, pointed value, and memory address of your pointer. What do you notice about the memory addresses? Guess the segment in which these variables exist (stack, heap, data, bss, or code; see [the drawing from Monday](https://cs.pomona.edu/classes/cs105/drawings/MonAug29-ComputerSystems.pdf)). Next, - use your pointer to change the value of the variable it points at, - prin the value, pointed value, and memory address of your poinrter, and - print the value and memory address of the original variable. # Swap function Now, - create two local variables in main, - write a swap function that swaps the values of two variables (using pointers), and - print the values before and after a swap. # Swap function for pointers Repeat the above exercise, but swap the values of two pointer-type variables. # Static variables Now, - write a function that has a static variable, and - print the value and memory addresses of this variable after calling the function a few times. What do you notice about the memory addresses? Guess the segment in which these variables exist (stack, heap, data, bss, or code; see [the drawing from Monday](https://cs.pomona.edu/classes/cs105/drawings/MonAug29-ComputerSystems.pdf)). # Dynamic memory Now, - create a variable the points to dynamically allocated memory, - print the value and memory addresses of this variable. What do you notice about the memory addresses? Guess the segment in which these variables exist (stack, heap, data, bss, or code; see [the drawing from Monday](https://cs.pomona.edu/classes/cs105/drawings/MonAug29-ComputerSystems.pdf)).