Lab 0: Warm-Up & Preview

Assigned
Monday, January 3, 2022
Due
Monday, January 10, 2022
Overview

Learning Objectives

Set up your computing environment for the rest of the quarter.
Compile and run C code on a Linux environment.
Observe C programming behaviors that will preview the topics covered in the later labs.

Be sure to read the Linux Tips page to set up your environment and pick a text editor before starting this lab.

First read the instructions on this page to get started; more instructions are found in the "Lab 0" Lesson on along with comments in the starter code.

Code for this lab
or in terminal:
wget https://courses.cs.washington.edu/courses/cse351/22wi/files/labs/lab0.c
Instructions

Editing Code

After acquiring the source file, you will need to openlab0.c in your text editor of choice. See the tutorials if you are unsure how to make edits.

The lab0.c file contains a number of comments explaining some basics of C (and their differences from Java). There are five different parts to this lab and you will need to modify or write some lines of code for each one. We recommend keeping a fresh copy of lab0.c around for reference (as you may lose track of all the changes you end up making).

In particular, it will be helpful for this lab (and for using C moving forward) if you take a little time to familiarize yourself with the printf function, which is used to output formatted messages to the console.

Compiling Code

The source file lab0.c won't do anything by itself; you need a compiler (specifically the GNU C compiler) to generate an executable from it. The GNU C compiler is available on the CSE VM, attu, and the instructional Linux machines in the CSE labs.

You will need to do the following only once at the start of the quarter:

Access the terminal in the CSE Linux environment (SSH into attu or open the Terminal in the CSE VM).
Execute the command touch ~/.gcc9 – this will create an empty, hidden file with the name .gcc9 in your home directory.
Log out and then log back in and make sure that the output of the command mentioned below matches.

Using any one of these machines, open a terminal and execute gcc -v. On attu, we see:

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/gcc-toolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/gcc-toolset-9/root/usr --mandir=/opt/rh/gcc-toolset-9/root/usr/share/man --infodir=/opt/rh/gcc-toolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.2.1-20191120/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.2.1 20191120 (Red Hat 9.2.1-2) (GCC)

The output tells you a bunch of the configuration options for the installation of GCC as well as the version number, which is 9.2.1. Assuming that you have saved lab0.c somewhere on your machine, navigate to that directory and then use GCC to compile it with the following command (the '$' below represents your command line prompt and is not part of what you should type):

$ gcc -g -Wall -std=c18 -o lab0 lab0.c

To deconstruct this command, note that the first word (gcc) is the compiler name and that it takes a bunch of "flags" that start with a dash (-):

-g tells the compiler to include debugging symbols
-Wall says to print warnings (the W) for all types (the all) of potential problems
-std=c18 says to use the C18 standard (the C language standard released in 2018)
-o lab0 instructs the compiler to output the executable code to a file called lab0
The ordering of these flags does not matter as long as -o lab0 is moved as a unit. The last word (lab0.c) is the source file being compiled.

During execution of that command, you can safely ignore warning about unused variables if you haven't made any changes yet. This warning would not be shown if you removed -Wall from the gcc command, but you will want -Wall to catch potential errors when you write code yourself.

Having executed the gcc command, you should now be able to see a file named lab0 in the same directory:

$ ls
lab0 lab0.c

Running Executables

The lab0 file is an executable file, which you can run using the command ./lab0. You should see:

$ ./lab0
Usage: ./lab0 <num>

In this case, the executable lab0 is expecting a command-line argument, which is text that is provided to the executable from the command-line when the program is run. In particular, lab0 wants a number from 1 to 5, corresponding to which part of the lab code you want to run. See main() in lab0.c for more details. For example (your values of p and q may differ):

$ ./lab0 1
*** LAB 0 PART 1 ***
x = 351
y = 410
p = 0x7ffdfe74369c
q = 0x7ffdfe743698
x & x = 351

Checking Your Work

With that, you should have everything you need to complete the assignment. Follow the instructions found on the associated Ed Lesson; you will want to work on the different parts of the lab in order (from 1 to 5). Each question can be answered and/or verified by appropriate edits to the source code.

Hot Tip!

Note that every time you want to test a code modification, you will need to re-run the gcc -g -Wall -std=c18 -o lab0 lab0.c command to produce an updated lab0 executable file.

Use the up and down keys to scroll through previous terminal commands you've executed.

Most of the code behaviors will seem inexplicable at this point, but our goal is that you will be able to explain to someone else what is going on by the end of this course! 😀
Submission
You can submit each question individually on the Lab 0 submission page in Ed Lessons. Ed will indicate which questions were answered correctly and which were answered incorrectly. This lab follows the homework policies (i.e., you have unlimited tries for each question in this lab).