views:

19

answers:

2

When providing the wrong number of arguments to printf():

printf("%s", "foo", "bar");

or when by providing arguments of the wrong type:

printf("%d", "foo");

gcc is able to warn about these mistakes:

$ gcc -Wformat printf_too_many_arguments.c
printf_warnings.c: In function `main':
printf_warnings.c:5: warning: too many arguments for format
printf_warnings.c:5: warning: too many arguments for format

$ gcc -Wformat printf_argument_of_wrong_type.c
printf_warnings.c: In function `main':
printf_warnings.c:5: warning: format `%d' expects type `int', but argument 2 has type `char *'
printf_warnings.c:5: warning: format `%d' expects type `int', but argument 2 has type `char *'

How to get such warnings with Visual Studio 2005?

-- dave

A: 

Unfortunately MSVC/Visual Studio does not support this.

See also http://stackoverflow.com/questions/2354784/attribute-formatprintf-1-2-for-msvc

nos
Thanks for the info. The lack of such warnings from Visual Studio is really bad.
A: 

You will need additional software to do this. Take a look at PC-Lint (http://www.gimpel.com/). It can find these kinds of errors (and much more [potential] errors as well).

Patrick

related questions