After acquiring the source file, you will need to open lab0.c in your 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 , which is used to output formatted messages to the console.
The source file lab0.c won't do anything by itself; you need a compiler (specifically the GNU C compiler) to generate to an executable from it.
The GNU C compiler is available on the , attu, and the instructional Linux machines in the CSE labs.
touch ~/.gcc9 – this will create an empty, hidden file with the name .gcc9 in your home directory.
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 me a bunch of the configuration options for my 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:
$ 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
-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
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
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.
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
(Tip: Use the up and down keys to scroll through previous terminal commands you've executed).
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 (you have unlimited tries for each question).
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! 😀