79514603

Date: 2025-03-17 12:48:53
Score: 1
Natty:
Report link
  1. printf is a variadic function

    printf takes a format string to interpret additional arguments. Since %d expects an integer, but none is provided, printf still tries to fetch an argument from the stack (or a register), leading to undefined behavior (UB).

  2. Why does it compile?

    C does not check the number of arguments passed to printf() at compile time (unless warnings are enabled).

  3. What happens at runtime?

    Since printf("%d") expects an integer but gets random data instead, the output is garbage or might cause a segmentation fault.

How to Fix it

Always provide the correct argument:

printf("%d", 42);  // Correct

Enable compiler warnings to catch such mistakes:

gcc -Wall -Wformat -o program program.c
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Starts with a question (0.5): is a
  • Low reputation (0.5):
Posted by: Alphin Thomas