We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Unit Tests

Up to this point, we've been checking the standard output of your code against our expected output. Now that you're familiar with functions, most of the lessons will be graded using unit tests.

µnit

In particular, we'll be using the µnit (munit) testing framework. It's a simple, lightweight testing framework for C.

Run vs. Submit

At Boot.dev, the Run button is for debugging. The Submit button mimics the idea of publishing your code for production use.

You should be debugging your code using the Run button. You should be adding printf() statements to your code to make sure it's doing what you think it's doing at different points in the code.

  • Write a line to calculate a value
  • printf() the value you calculated
  • Run the code
  • Did it print what you expected? If not, fix it
  • Repeat

You will never lose XP or be penalized on Boot.dev for using the run button. However, there are consequences for submitting broken code, just like there are career consequences for pushing broken code to your users!

Like how edge cases can creep up on you after shipping code to production, some test cases are only executed on Submit

File Layout

Take a look at the main.c file. You'll notice it's read-only: you can't change the tests (that would make it too easy to cheat- ha!). It #includes exercise.h at the top.

Open exercise.h and you'll see the function prototype (signature) of the function you need to write. In C:

  • .c files contain the implementation (c code).
  • .h files are header files that contain the function prototypes.

To import code from another file, you include the .h file.

  • exercise.c includes exercise.h.
  • main.c includes exercise.h.

This allows main.c to call the functions implemented in exercise.c.

Assignment

Write the get_average (mathematical average) function in exercise.c based on the function prototype expected in exercise.h.

Tips

  • The + operator adds two numbers: 1 + 2 is 3.
  • The / operator divides two numbers, however:
    • if both numbers are integers, integer division is performed. The result will be an integer.
    • if either number is a float, floating point division is performed. The result will be a float.