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/01/

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 01 main.c Code Diff
UNCHANGED This step does not change this file.
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
printf("\n");
printf("Algorithm: the average of grades\n");
printf("1. Add up all the grades.\n");
printf("2. Count how many grades there are.\n");
printf("3. Divide the sum by the count.\n");
printf("4. Print the answer.\n");
printf("\n");
printf("Grade\tMeaning\n");
printf("10\t\"excellent\"\n");
printf("5\t\"satisfactory\"\n");
printf("File: C:\\course\\practice\\01\\main.c\n");
printf("Done: 100%%\n");
return 0;
}