7.6.4.1 Options to Control the Amount and Types of Warnings

The following options control the amount and kinds of warnings produced by the compiler.

Table 7-8. Warning/Error Options Implied by -Wall
OptionDefinition
-fsyntax-onlyCheck the code for syntax, but don’t do anything beyond that.
-wInhibit all warning messages.
-WallAll of the -W options listed in this table combined. This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros.
-Wchar-subscriptsWarn if an array subscript has type char.
-Wcomment

-Wcomments

Warn whenever a comment-start sequence /* appears in a /* comment, or whenever a Backslash-Newline appears in a // comment.
-Wdiv-by-zeroWarn about compile-time integer division by zero. To inhibit the warning messages, use -Wno-div-by-zero. Floating point division by zero is not warned about, as it can be a legitimate way of obtaining infinities and NaNs.

(This is the default.)

-Werror-implicit-
function-declarationGive an error whenever a function is used before being declared.
-WformatCheck calls to printf and scanf, etc., to make sure that the arguments supplied have types appropriate to the format string specified.
-WimplicitEquivalent to specifying both -Wimplicit-int and 
-Wimplicit-function-declaration.
-Wimplicit-function-
 declarationGive a warning whenever a function is used before being declared.
-Wimplicit-intWarn when a declaration does not specify a type.
-WmainWarn if the type of main is suspicious. main should be a function with external linkage, returning int, taking either zero, two or three arguments of appropriate types.
-Wmissing-bracesWarn if an aggregate or union initializer is not fully bracketed. In the following example, the initializer for a is not fully bracketed, but that for b is fully bracketed.

int a[2][2] = { 0, 1, 2, 3 };

int b[2][2] = { { 0, 1 }, { 2, 3 } };

-Wmultichar

-Wno-multichar

Warn if a multi-character char constant is used. Usually, such constants are typographical errors. Since they have implementation-defined values, they should not be used in portable code. The following example illustrates the use of a multi-character char constant:
char
xx(void)
{
return('xx');
}
-WparenthesesWarn if parentheses are omitted in certain contexts, such as when there is an assignment in a context where a truth value is expected, or when operators are nested whose precedence people often find confusing.
-Wreturn-typeWarn whenever a function is defined with a return-type that defaults to int. Also warn about any return statement with no return-value in a function whose return-type is not void.
-Wsequence-pointWarn about code that may have undefined semantics because of violations of sequence point rules in the C standard.

The C standard defines the order in which expressions in a C program are evaluated in terms of sequence points, which represent a partial ordering between the execution of parts of the program: those executed before the sequence point and those executed after it. These occur after the evaluation of a full expression (one which is not part of a larger expression), after the evaluation of the first operand of a &&, ||, ? : or , (comma) operator, before a function is called (but after the evaluation of its arguments and the expression denoting the called function), and in certain other places. Other than as expressed by the sequence point rules, the order of evaluation of subexpressions of an expression is not specified. All these rules describe only a partial order rather than a total order, since, for example, if two functions are called within one expression with no sequence point between them, the order in which the functions are called is not specified. However, the standards committee has ruled that function calls do not overlap.

It is not specified, when between sequence points modifications to the values of objects take effect. Programs whose behavior depends on this have undefined behavior; the C standard specifies that “Between the previous and next sequence point, an object shall have its stored value modified, at most once, by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.” If a program breaks these rules, the results on any particular implementation are entirely unpredictable.

Examples of code with undefined behavior are a = a++;, a[n] = b[n++] and a[i++] = i;. Some more complicated cases are not diagnosed by this option and it may give an occasional false positive result, but in general it has been found fairly effective at detecting this sort of problem in programs.

-WswitchWarn whenever a switch statement has an index of enumeral type and lacks a case for one or more of the named codes of that enumeration. (The presence of a default label prevents this warning.) case labels outside the enumeration range also provoke warnings when this option is used.
-Wsystem-headersPrint warning messages for constructs found in system header files. Warnings from system headers are normally suppressed, on the assumption that they usually do not indicate real problems and would only make the compiler output harder to read. Using this command line option tells the compiler to emit warnings from system headers as if they occurred in user code. However, note that using -Wall in conjunction with this option will not warn about unknown pragmas in system headers; for that, -Wunknown-pragmas must also be used.
-WtrigraphsWarn if any trigraphs are encountered (assuming they are enabled).
-WuninitializedWarn if an automatic variable is used without first being initialized.

These warnings are possible only when optimization is enabled, because they require data flow information that is computed only when optimizing.

These warnings occur only for variables that are candidates for register allocation. Therefore, they do not occur for a variable that is declared volatile, or whose address is taken, or whose size is other than 1, 2, 4 or 8 bytes. Also, they do not occur for structures, unions, or arrays, even when they are in registers.

Note that there may be no warning about a variable that is used only to compute a value that itself is never used, because such computations may be deleted by data flow analysis before the warnings are printed.

-Wunknown-pragmasWarn when a #pragma directive is encountered which is not understood by the compiler. If this command line option is used, warnings will even be issued for unknown pragmas in system header files. This is not the case if the warnings were only enabled by the -Wall command line option.
-WunusedWarn whenever a variable is unused aside from its declaration, whenever a function is declared static but never defined, whenever a label is declared but not used, and whenever a statement computes a result that is explicitly not used.

In order to get a warning about an unused function parameter, both -W and -Wunused must be specified.

Casting an expression to void suppresses this warning for an expression. Similarly, the unused attribute suppresses this warning for unused variables, parameters and labels.

-Wunused-functionWarn whenever a static function is declared but not defined or a non-inline static function is unused.
-Wunused-labelWarn whenever a label is declared but not used. To suppress this warning, use the unused attribute (see section Variable Attributes).
-Wunused-variableWarn whenever a local variable or non-constant static variable is unused aside from its declaration. To suppress this warning, use the unused attribute (see section Variable Attributes).
-Wunused-valueWarn whenever a statement computes a result that is explicitly not used. To suppress this warning, cast the expression to void.