Assignment 6 - C Intro 1

Due: Monday, April 13, 2026, at 10pm

Starter code: a6_c_1.zip
Upload solutions via Gradescope


Goals

This assignment is designed to help you with the following:

  • getting started working with C

Collaboration policy

For this assignment, you may work alone or with a partner, but you must type up all of the code yourself. (It is therefore unexpected for two code submissions to be completely identical.)

You may also discuss the assignment at a high level with other students.

You should list any student with whom you discussed the assignment, and the manner of discussion (high level, partner, etc.) in your readme.txt file.

If you work alone, you should say so instead.


Assessment

Like our first assignment, we’ll work our way towards style expectations. For now, you just need to make minor code changes as you explore working with C.

Core requirements:

  • all CR tests pass
  • your code is not hyper-tailored to pass the tests
  • readme.txt contains your collaboration statement, sources, and reflection

Advanced requirements:

  • satisfy the Core requirements
  • all AD tests pass

Getting started

As before, you should download the starter files, unzip that file, and copy the folder to where you’ll work (probably your ProgrammingLanguages folder). Refer to the Assignment 1 instructions if you’re unsure what to do.


Part A: Hello, C!

Part A1: Run your first program

Create a new file called hello.c in VS Code. Then, copy and paste (or better yet, type up!) the content from this hello.c file into that file. This program (unsurprisingly) is supposed to print “Hello, world!” to the screen. Take a look around the file. Single-line comments use // and multi-line comments use /**/, just like in Java or Kotlin.

Next, compile the program to a binary executable by running the following in the terminal:

clang -o hello hello.c

This command says to use clang to compile the file hello.c to the output file hello. You should be able to see the file hello in the file list in VS Code, and also if you type ls in the terminal.

Try running it at the command prompt:

./hello

If this worked, you should see “Hello, world!” output to the terminal.

Part A2: Exercise 1

Change the program to print the following:

Hello, CS 251!

Run the program again. When complete, you can also run ./test-cr, which will run the automated CR tests.

There are three sections of CR tests. If all went well, the HelloTests should pass. You’ll know that happened by the fact that you don’t see any errors or failures for HelloTests, and the last couple of output lines should look something like:

Ran 3 tests in 1.192s

FAILED (errors=2)

Your number of seconds will differ, but it should say it ran 3 tests and only 2 of them had errors. If it says FAILED (failures=1, errors=2) that means your printing in hello.c isn’t quite right.

Part B: Printing and user input in C

Part B1: Printing

The first non-commented line of hello.c is #include <stdio.h>. This directs the C compiler (or technically, a subprocess called the C preprocessor) to include a header file named stdio.h, which contains the function signatures for standard IO functions, including printf.

The documentation for most of the built-in C functions can be found in the UNIX manual (“man pages”). Take a look at the documentation for printf in particular. This tells you which header file to include to get access to that function (in this case, stdio.h), as well as documenting the interface and behavior of the function of interest. In this case, the man page also includes several similarly named functions, such as sprintf and fprintf. Read through the first few paragraphs of the man page for printf and skim the rest to get a sense of what these pages look like. There are examples at the end, which is often the most useful part.

Like Python’s string formatting operator, printf accepts a string that includes format specifiers to print integers, floats, etc. To see more about how printf and other operations work, create a file named printing.c in your directory, and then copy the contents of this printing.c file. It contains a number of different examples of print formats.

To compile and run your code, go back and look what we did above for hello.c and change it accordingly.

Part B2: Exercise 2

Add code to printing.c that subtracts the value 15 from 87 and prints the result, together with an appropriate message. Check the PrintingTests section of the results from running ./test-cr to see if you’ve got it working correctly.

Here are some common C types and their printf formatting specifications:

  • %c: char
  • %d or %i: int
  • %ld or %li: long
  • %f: float
  • %s: string (really it’s a char *)
  • %p: poitner (e.g., int *)

Part B3: User input

You can get user input using the scanf function (check out the man page here):

printf("Please enter an int: "); // what happens if we add \n at the end?
int i;
scanf("%d", &i);
printf("You entered: %d\n", i);  // why have \n here?

The first argument to scanf is a string containing formatting specification(s) for the type of input(s) expected. In this case we expect an int.

The second argument may look pretty weird to you. How is scanf able to modify the value of i? What’s that ampersand symbol & doing there? The answer is that we’re actually passing a pointer to i’s location, not i itself, so scanf can follow that pointer and update the value stored there, changing i. We’ll talk much more about pointers in class.

Part B4: Exercise 3

Write a program temperature.c that asks the user for a temperature in Fahrenheit and prints the temperature converted to Celsius. Assume that the input is handled as a float instead of as an int.

The relevant formula is:

temp_c = (temp_f - 32) * 5/9

For example, where’s what a sample run of temperature.c might look like:

What is the temperature in degrees Fahrenheit? 25.1
25.100000 degrees Fahrenheit is -3.833333 degrees Celsius.

(If you’re interested, there are variants of the formatting specifiers that limit the number of zeroes. Read up on that further if you like.)

Check the TemperatureTests section of the tests after running ./test-cr to see if you have the output correct.

Part C: Loops and conditional statements in C

Part C1: Booleans in C

C has roughly the same syntax for if statements, for loops, while loops, and do/while loops as Kotlin and Java. However, there is a critical and very subtle difference with if statements that you should be aware of. C doesn’t have boolean values, exactly, it just has integers. So, just like in Scheme, 0 is false and anything non-zero is true. So, the following potentially confusing snippet is completely legal C code:

int x = 1;
int y = 0;
if (x - y)
{
    printf("These values are different!\n");
}
else
{
    printf("These values are the same number!\n");
}

If you want to make your code more clear, you can use the library stdbool.h as follows:

#include <stdbool.h>

int main()
{
    bool x = true;

    return 0;
}

However, this is just syntactic sugar, which is a fancy phrase to indicate that we haven’t changed the underlying language, we’ve just changed the syntax a little bit. With the above, x is really just an int and true is really just a 1. Why does this matter? Try running the following SUPER PROBLEMATIC BUGGY code and see what happens. (Maybe put this in a separate file buggy.c so it doesn’t get mixed in with your homework code.)

#include <stdio.h>

int main()
{
    int x = 3;
    if (x = 5) // assignment can be used as a statement or expression
    {
        printf("x must be 5, even though I assigned it to 3.\n");
    }
}

However, clang does at least try to warn you when you do this.

Part C2: Exercise 4

What happens if the user enters and impossible temperature in Exercise 3? Absolute zero, which is the coldest temperature anything can possibly be, is -459.67 Fahrenheit. Modify temperature.c so that if a temperature lower than that is input, it displays the text Invalid temperature. This exercise is the only one in the AD tests. Run ./test-ad to see if you have this part correct.


Testing your work

As with the previous assignments, there are CR and AD tests. Look at the instructions from A2 if you want a refresher on how to run this tests.


Submitting your work

For this assignments, you will submit four separate files. You are strongly encouraged to run ./zipitup to use that script to gather your files for Gradescope.

C code

The files hello.c, printing.c, and temperature.c should contain your functions. Make sure to add a comment above each of your functions (including any helper functions you choose to write) to describe its behavior at a high level (e.g., what it takes as input and what it returns as output).

Readme

For each assignment for this course you will also need to submit a file readme.txt in which you should write:

  1. Your collaborations with anyone on the assignment
  2. Your use of any outside sources on the assignment
  3. Your reflection

You should be specific about your collaborations and sources – they shouldn’t just be empty or lists of names of people. If you worked alone and/or did not use any outside sources, you should say so.

For your reflection, spend a couple of sentences answering the following:

  • Were there any particular issues or challenges you dealt with in completing this assignment?
  • How long did you spend on this assignment?

Submitting to Gradescope

As always, use ./zipitup to gather your files. Submit the resulting .zip file to Gradescope at this link.

Note that it is possible (although unlikely) that the tests will pass on your computer but fail on Gradescope. If this happens, it’s probably because you coded something in an unusual way. Double-check that you’re submitted to the correct assignment and then check if any error messages can help you troubleshoot. If you’re still having issues, check with Tanya.