Skip to the task
Layout
Back to classic view

You are looking ahead. Your place is still task 1: Functions.

READTask 2 of 23 · step 2: Contracts and assertContracts and assert
Contracts and assert in practice/02/

The precondition and the postcondition, assert and its message, Release mode, and what doesn't go into an assert.

Details & hint

The contract

A function is an agreement between its author and whoever calls it:

  • the precondition is what the caller must make sure of, e.g. "x is not negative";
  • the postcondition is what the function promises when the precondition holds, e.g. "it returns the nearest whole number".

Write the contract as a comment above the function:

/* Rounds to a whole number.
   Before: x >= 0.
   After: the nearest whole number; x.5 is rounded up. */
int round_to_int(double x) {

The precondition isn't made up: in topic 2 you saw that (int)(x + 0.5) rounds a negative number wrongly.

assert: a precondition the program checks

Nobody checks a comment. assert does:

#include <assert.h>

int round_to_int(double x) {
    assert(x >= 0);
    return (int)(x + 0.5);
}

While the condition is true, assert does nothing. When it is false, the program stops at once and says where. For example, if the program of step 4 called round_to_int(-2.7):

Assertion failed: x >= 0, file C:/.../practice/05/main.c, line 56

Process finished with exit code 3

So the bug is caught where it started, not a hundred lines later, when the wrong number has already spread through the whole program.

assert is for the programmer's mistakes

A user typing 11 is not a bug in the program: you have to expect it, and in topic 3 you asked again with while. assert is for another case: when the code calls a function with something it can't accept, e.g. you forgot to check the grade before print_in_words(11).

What is wrong How to handle it
the user typed an invalid value while and ask again
the code broke a function's precondition assert stops the program

Tests with assert

assert also works for tests: a function that calls your functions with known data and checks the result.

void run_tests(void) {
    assert(round_to_int(0) == 0);
    assert(round_to_int(7.49) == 7);
    assert(round_to_int(7.5) == 8);
}

What to test:

  • the usual case, to see that the function works at all;
  • the edges: 0, 7.49 and 7.5, 1 and 10; bugs hide there most often;
  • both sides of the answer: is_valid(10) and !is_valid(11).

When a test fails, assert prints the line that is false, so you see straight away which case doesn't work.

In Release mode assert is switched off

CMake's Release configuration defines NDEBUG, and then every assert disappears from the program. So never put inside an assert something that has to happen:

assert(read_int("How many? ") > 0);   /* in Release the question is gone */

The course's tests are always compiled with assert switched on.

practice 02 main.c Code Diff
UNCHANGED This step does not change this file.
#include <limits.h>
#include <stdio.h>
#include "input.h"
int main(void) {
const int current_year = 2026;
int study_year = 1;
char group = 'A';
int birth_year = read_int("Year of birth: ");
int g1 = read_int("Grade 1: ");
int g2 = read_int("Grade 2: ");
int g3 = read_int("Grade 3: ");
double average = (g1 + g2 + g3) / 3.0;
int age = current_year - birth_year;
printf("Year of study: %d\n", study_year);
printf("Group: PI25%c\n", group);
printf("Age: %d years\n", age);
printf("Average: %.2f\n", average);
printf("In memory: int %zu B, double %zu B, char %zu B\n", sizeof study_year, sizeof average,
sizeof group);
printf("\n");
printf("Largest int: %d\n", INT_MAX);
long long century = 100LL * 365 * 24 * 60 * 60;
printf("100 years: %lld s\n", century);
printf("0.1 + 0.2 = %.17f\n", 0.1 + 0.2);
return 0;
}