Last Updated: 2019-07-09 Tue 21:37

CSCI 2021 Lab01: C Basics

CODE DISTRIBUTION: lab01-code.zip

  • Download the code distribution every lab
  • See further setup instructions below

CHANGELOG: Empty

1 Rationale

C programming is as close as most programmers get to the bare metal of computer systems and is an essential tool in any programmers toolkit. Completing this lab will cover principles in C of editing, compilation, running, basic data types, functions, and control.

1.1 Associated Reading / Preparation

Prior programming experience is presumed: students not familiar with variable types, loops, conditionals, functions, etc. should review them quickly.

Basic familiarity with the Unix command line environment such as can be found on lab machines is also presumed. If you have never worked in this type of environment, make sure to do some self-study.

Study any good C resource/tutorial particularly those listed on the course web site or the optional text and C programming:

C Programming Language Second Edition by Brian Kernighan and Dennis M. Ritchie, Prentice Hall 1988

Also make sure to consult the Course Syllabus prior to taking the Lab Quiz.

1.2 Grading Policy

Credit for this lab is earned by taking the associate Lab Quiz which is linked under Canvas / Quizzes. The quiz will ask that you upload your completed QUESTIONS.txt document and then take a short quiz on the material covered in the lab.

See the full policy in the syllabus.

2 Codepack

The codepack for the lab contains the following files:

File Description
QUESTIONS.txt Questions to answer
age.c C file for Problem 1
collatz_main.c C file for Problem 2
collatz_funcs.c C file for Problem 2
collatz.h C file for Problem 2

3 Accessing Unix Environments for C Code

To answer the questions for this lab, you will need to be able to compile and run C programs. A Unix environment is the best place to do this and the sections below describe ways to access Unix environments.

3.1 Vole for Graphical Login: http://vole.cse.umn.edu

CSE Labs provides a slick graphical login to Linux machines through a web browser via its VOLE system. Navigate to http://vole.cse.umn.edu, enter your login credentials, and initial a graphical login.

3.2 SSH to Lab Machines

CSE Labs allows you to remotely log into a variety of machines using the secure shell or SSH for short. For this you will need an SSH tool

  • Mac: open a terminal and use the ssh command
  • Windows: examine these choices of which PuTTY is a classic favorite.

Select a machine name from the big list of machines and log in. My go-to is

apollo.cselabs.umn.edu

3.3 Physical Access

You can sit down at a variety of machines managed by CSE Labs which are listed here: https://cseit.umn.edu/computer-classrooms

Be mindful of the times these labs are open and which labs have Unix machines listed as Ubuntu 16.04. Keller 4-250 is likely the most popular.

3.4 Your own machine

It is fun and profitable to set up your own machine to compile code for the class.

Virtual Machines

Running Virtual Machine software allows you to have a "guest" operating system, likely free Linux, which has separate software from the "host" OS. My go-to for a long time was running VirtualBox with a Linux distribution as the guest OS.

In almost all cases, compiling and running code on a Linux VM (virtual machine) will behave identically to a "native" Linux.

Mac OSX

By default there may be a program called gcc on Mac OSX but it is likely NOT the compiler we'll use by default: see the following discussion which also suggests making use of HomeBrew to manage free software on Mac OSX such as a "real" gcc.

NOTE: Assembly produced by gcc will target the Mac OSX architecture and is not likely to work on lab Linux machines where we will grade. Make sure to verify your code works in those environments as well.

Windows

Windows does not have Unix tools by default so an environment must be installed.

The suggested method is to make use of the Windows Subsystem for Linux on Windows 10:

  • Install a flavor of Linux such as Ubuntu (Video Tutorial)
  • Start a linux command line via the bash program
  • Follow the Linux installation instructions appropriate to the distro (see Linux/Unix instructions)

Alternatively, the venerable Cygwin project provides a Unix-like environment including gcc and associated tools.

NOTE: Assembly produced by gcc will target the Windows architecture and is not likely to work on lab Linux machines where we will grade. Make sure to verify your code works in those environments as well.

4 Basic Navigation

On logging into the machine you will work on, you will need to make sure that you get the codepack linked at the top associated with the lab and unzip it which will create a directory called lab01-code with C files for you to analyze. To compile and run these, open a terminal and navigate to the directory lab01-code. This is usually done by issuing a series of "change directory" or cd commands as in the following brief demo.

kauffman [~]$ cd csci2021

kauffman [~/csci2021]$ ls
lecture-stuff/ lab01-code.zip lab01-code/

kauffman [~/csci2021]$ cd lab01-code/

kauffman [~/csci2021/lab01-code]$ ls
age.c  collatz_funcs.c  collatz.h  collatz_main.c  QUESTIONS.txt

If you are inexperienced working with a terminal (also called command line or shell), then the following tutorial may give you the basics to get through most of the course.

http://www.ee.surrey.ac.uk/Teaching/Unix/

5 Questions

Analyze the files in the provided codepack and answer the questions given in QUESTIONS.txt.

                           __________________

                            LAB 01 QUESTIONS
                           __________________


- Name: (FILL THIS in)
- NetID: (THE kauf0095 IN kauf0095@umn.edu)

Answer the questions below according to the lab specification. Write
your answers directly in this text file and submit it to complete the
lab.


PROBLEM 1 `age.c'
=================

A
~

  Examine the source code for `age.c'. Compile it using the following
  invocation of `gcc'
  ,----
  | gcc age.c
  `----
  This should create the executable `a.out'. Run this program using the
  invocation.
  ,----
  | ./a.out
  `----
  Perform several runs of the program with the following values and
  paste your output in the space below.
  - Enter 16
  - Enter 18
  - Enter 25
  - Enter 44


B
~

  Analyze the code for `age.c' and describe a flaw in the conditional
  structure towards the end that prevents the output:
  ,----
  | You can vote, drink, and be president.
  | Try all three at once!
  `----
  from being printed.

  Alter the code to fix this bug so that for ages 35 and larger, the
  above text is printed. Paste your fixed code for the conditional below
  and test it by recompiling and showing a demo run.


C
~

  Attempt to enter some absurd ages for the age computation.
  - Enter 5000
  - Enter -5000
  Describe anything strange that seems to be happening based your
  understanding of how basic arithmetic is supposed to work.

  If you happen to know WHY this strangeness is happening, describe it
  below.  If not, you will find out soon.


D
~

  Describe which function is used to print information to the screen.
  Describe how it seems to work to substitute values into output and
  what *format specifier* indicates an integer should be substituted.


E
~

  Describe what function is used to read typed input interactively from
  a user in the `age.c' program.  Describe anything that seems strange
  about this function or its arguments.

  We will learn in not long why this bit of strangeness is necessary.


PROBLEM 2 Collatz
=================

A
~

  Examine and compile the code associated with the collatz
  program. There are three files associated with this program.
  - `collatz_funcs.c' which defines two utility functions for computing
    the Collatz sequence
  - `collatz_main.c' which defines a `main()' function to compute a
    Collatz sequence
  - `collatz.h' header file which declares functions in
    `collatz_funcs.c' so that they are known to `collatz_main.c'

  To compile the program, use the following invocation of `gcc'
  ,----
  | gcc -o collatz collatz_funcs.c collatz_main.c
  `----
  This should create the program `collatz' which can be run with
  ,----
  | ./collatz
  `----

  Do so and enter inputs
  - Starting integer 7
  - Show output: 1

  Paste the output below.


B
~

  Determine what the "dash-O" option used above for `gcc -o' does. For
  example, what happens if one runs
  ,----
  | gcc -o GLIPGLOP collatz_funcs.c collatz_main.c
  `----
  instead.  You may wish to use the `ls' command to list the files in
  the current directory.

  Describe what happens if you omit this option `-o' when compiling as
  in
  ,----
  | gcc collatz_funcs.c collatz_main.c
  `----


C
~

  Attempt to compile only the file `collatz_main.c' by doing
  ,----
  | gcc -o something collatz_main.c
  `----

  This should result in an error. Show the output of that error and
  determine why the compilation fails.


D
~

  Attempt to compile only the file `collatz_funcs.c' by doing
  ,----
  | gcc -o something collatz_funcs.c
  `----

  This should result in an error. Show the output of that error and
  determine why the compilation fails.


Review Course Syllabus
======================

  Make sure to review the Course Syllabus to acquaint yourself with
  course policies such as the following.
  - The PRIME DIRECTIVE to preserve academic integrity
  - Fair collaboration with other students
  - Late submission policies on Assignments and Lab work
  - Grading criteria and weighting on exams/assignments/labs.

6 What to Understand

Ensure that you understand

  • Basic C syntax for functions, conditions, loops
  • How to compile C programs on the command line with gcc and some errors that arise due to compilation mistakes
  • Some inkling about the limits of binary number representations
  • A basic understanding of the course Syllabus

Author: Chris Kauffman (kauffman@umn.edu)
Date: 2019-07-09 Tue 21:37