Last Updated: 2022-09-01 Thu 11:49

CSCI 2021 HW01: C Basics


  • Download the code distribution
  • See further setup instructions below


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 HW will cover principles in C of editing, compilation, running, basic data types, functions, and control.

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 is also presumed. If you have never worked in this type of environment, make spend some time getting oriented with staff help. Consult the Accessing Unix/Linux Programming Environments guide for some additional help.

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 Quiz associated with the HW. Some questions on the Quiz may test knowledge of the course syllabus.

Grading Policy

Credit for this HW is earned by taking the associated HW Quiz which is linked under Gradescope. The quiz will ask similar questions as those that are present in the QUESTIONS.txt file and those that complete all answers in QUESTIONS.txt should have no trouble with the quiz.

Homework and Quizzes are open resource/open collaboration. You must submit your own work but you may freely discuss HW topics with other members of the class.

See the full policies in the course syllabus.

2 Codepack

The codepack for the HW 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 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


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


                            HW 01 QUESTIONS

Write your answers to the questions below directly in this text file to
prepare for the associated quiz. Credit for the HW is earned by
completing the associated online quiz on Gradescope.

PROBLEM 1 `age.c'


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


  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.


  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.


  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.


  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


  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.


  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
  | gcc collatz_funcs.c collatz_main.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.


  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 (projects, HW, quizzes, lab
  - Grading criteria and weighting on exams/assignments

Author: Chris Kauffman (
Date: 2022-09-01 Thu 11:49