Lab 0: Warm-Up & Preview

Assigned
Monday, March 27, 2023
Due
Monday, April 3, 2023
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.

Note that your objective is simply to observe behaviors in this lab. The code behaviors are indended to be surprising and you will likely not understand what's going on yet – that's what the rest of this course is for!
Code for this lab
or in terminal:
wget https://courses.cs.washington.edu/courses/cse351/23sp/files/labs/lab0.c
wget is a program which retrieves files from web servers. Entering the command above in your attu/CSE VM terminal will directly download lab0.c onto your Linux system!
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.

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=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-host-pie --enable-host-bind-now --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.rockylinux.org/ --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 --without-isl --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_64=x86-64-v2 --with-arch_32=x86-64 --build=x86_64-redhat-linux --with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.3.1 20220421 (Red Hat 11.3.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 11.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).