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.
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.
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=/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
-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.