liveBook · Manning

Compiler diagnostic

As explained in Modern C

A compiler diagnostic is a message reporting a suspected problem, warning, or other condition found while processing source code.

A compiler diagnostic reports a suspected problem or other condition in source code. Warnings may still produce an executable, while a fatal diagnostic can stop compilation. section 1.2

Read, fix, and recompile

A flawed C program triggers warnings about main, printf, and returning a value from void main. Clang adds a precise header hint and rejects the program; fix each reported line, then recompile. TAKEAWAY: a C program should compile cleanly without warnings. section 1.2

 0   > clang -Wall -o getting-started-badly bad.c
 1   bad.c:4:1: warning: return type of 'main' is not 'int' [-Wmain-return-type]
 2   void main() {
 3   ^
 4   bad.c:16:6: warning: implicitly declaring library function 'printf' with type
 5         'int (const char *, ...)'
 6        printf("element %d is %g, \tits square is %g\n", /*@\label{printf-start-badly}*/
 7        ^
 8   bad.c:16:6: note: please include the header <stdio.h> or explicitly provide a declaration
 9         for 'printf'
10   bad.c:22:3: error: void function 'main' should not return a value [-Wreturn-type]
11     return 0;
12     ^      ~
13   2 warnings and 1 error generated.


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":10,\"ch\":1},{\"line\":10,\"ch\":2}],[{\"line\":11,\"ch\":14},{\"line\":11,\"ch\":15}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":4,\"ch\":11},{\"line\":4,\"ch\":12}],[{\"line\":8,\"ch\":11},{\"line\":8,\"ch\":12}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":1}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":1}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":1}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":1}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":2}],[{\"line\":10,\"ch\":11},{\"line\":10,\"ch\":12}],[{\"line\":12,\"ch\":1},{\"line\":12,\"ch\":2}],[{\"line\":13,\"ch\":5},{\"line\":13,\"ch\":6}],[{\"line\":3,\"ch\":1},{\"line\":3,\"ch\":2}],[{\"line\":10,\"ch\":14},{\"line\":10,\"ch\":15}],[{\"line\":13,\"ch\":1},{\"line\":13,\"ch\":2}],[{\"line\":1,\"ch\":11},{\"line\":1,\"ch\":12}],[{\"line\":4,\"ch\":1},{\"line\":4,\"ch\":2}],[{\"line\":5,\"ch\":1},{\"line\":5,\"ch\":2}],[{\"line\":4,\"ch\":12},{\"line\":4,\"ch\":13}],[{\"line\":6,\"ch\":1},{\"line\":6,\"ch\":2}],[{\"line\":8,\"ch\":12},{\"line\":8,\"ch\":13}],[{\"line\":7,\"ch\":1},{\"line\":7,\"ch\":2}],[{\"line\":8,\"ch\":1},{\"line\":8,\"ch\":2}],[{\"line\":9,\"ch\":1},{\"line\":9,\"ch\":2}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":2}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":2}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":2}]]"}
!@%STYLE%@!

Inspect the diagnostic listing to compare warning, note, and error messages with the source lines they identify. section 1.2

How Modern C covers it

How deep it goes
Distinguishes warnings from fatal errors and demonstrates line-specific hints, but does not explain diagnostic implementation.
Examples
Uses a flawed C program and gcc and clang output concerning main, printf, and return statements.
What it assumes
Assumes basic C source and compilation; teaches attention to warnings, compiler hints, and clean builds through a concrete program.
What you can do after
Diagnose and correct reported C source problems by reading locations, messages, notes, and severity.
How it connects
Connects compiler diagnostics with warnings, errors, source-code lines, executables, and compiler options.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.