'What types of error when using the wrong format specifier in printf

Floating-point format specifier is used in printf by the argument has a integer number. What kind of error is it? Is it a run-time error or a syntax error?

error message is shown below

clang-7 -pthread -lm -o main main.c
main.c:21:46: warning: format specifies type 'int' but the argument
      has type 'double' [-Wformat]
  ...and the processed value is %d\n", processed_value); // wron...
                                ~~     ^~~~~~~~~~~~~~~

Solution 1:[1]

It is not a syntax error, because the program still follows the syntactic and semantic structure of a C program. But it is an invalid use of a standard library function, that has been specified as having undefined behaviour. As a courtesy new C compilers diagnose format string problems wherever they can.

Solution 2:[2]

This is describing a runtime error.

The compiler is warning you, though the print format expects an int (because of the %d), you passed it a double. If you meant to pass it a double, then change the %d to a %f.

Since it's only a warning and not an error, the program still compiled. However, as M.M mentioned in the comments, it will result in undefined behavior when you run it and, therefore, you should resolve it.

Solution 3:[3]

This is a static semantic error which is picked up by the semantic analizer at compile time. The semantic analizer is the third stage of compilation, after the lexical analyzer and syntax analyzer.

A big clue is that C shows the warning at compile time. This doesn't mean that it won't also be a problem at run-time. But it is telling you about it during compilation.

It is not a syntax error, because it is valid syntaxically for C grammar. It is a semantic error because it is incorrect in terms of it's known meaning. Sometimes syntax and static (compile time) semantic errors can be quite difficult to distinguish and perhaps you will have to ask someone who implemented the language or studied it closely to get an accurate read. For instance, if you had left out the format specifier altogether you could argue it is more of a syntax error as the printf() function definition requires a format specifier where a variable is given. However these are very fine distinctions.

However, even if if this code doesn't necessarily break at run-time, but if it gives you a different than expected result, then you have information from the compiler to check against.


This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Antti Haapala -- Слава Україні
Solution 2
Solution 3 BasilBrush