Skip to the task
Layout
Back to classic view

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

READTask 13 of 23 · step 6: Common mistakesCommon mistakes
Common mistakes in practice/02/

Eight mistakes with types, formats and input.h. When the program doesn't build or calculates oddly, check these first.

Details & hint

When the program doesn't build or calculates something other than you expect, check this list first.

Common mistakes
  • A wrong format. format '%d' expects argument of type 'int', but argument 2 has type 'double': a double is printed with %f, a long long with %lld. GCC writes the right format under the error.
  • Integer division. (g1 + g2 + g3) / 3 throws the fraction away before it is stored in the double. At least one side must be a double: 3.0 or (double)sum.
  • Overflow. long long x = 100 * 365 * 24 * 60 * 60; is still worked out with int. Write 100LL * ....
  • A comma instead of a dot. In code, double x = 7,5; gives expected identifier or '(' before numeric constant. When typed as input, 7,5 gets Not valid: enter a number with a dot, e.g. 7.5.
  • A forgotten #include "input.h". implicit declaration of function 'read_int'.
  • A forgotten target_link_libraries. Compiling succeeds, but the linker reports undefined reference to 'read_int': CMakeLists.txt is missing target_link_libraries(... PRIVATE course_input).
  • An unused variable. unused variable 'average' [-Werror=unused-variable]: either use it or delete it.
  • "A" instead of 'A'. %c expects one character in single quotes. With "A" GCC reports argument 2 has type 'char *'.
practice 02 CMakeLists.txt Code Diff
UNCHANGED This step does not change this file.
add_executable(p02_card main.c)
target_link_libraries(p02_card PRIVATE course_input)