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 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;
}