Skip to the task
Layout
Back to classic view

You are looking at lesson 2. Your place is still lesson 4, task 1: Style.

READTask 13 of 23 · step 6: Common mistakes

Common mistakes

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

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 *'.