gcc limits and -Wformat



#include <stdio.h>
#include <limits.h>
int main (void)
{
unsigned long ul = LONG_MAX;
short int si = SHRT_MIN;
printf ("%dn", ul);
printf ("%sn", si);
return 0;
}


$gcc printme.c
$./a.out
-1
Segmentation fault
$gcc printme.c -Wformat
printme.c: In function ‘main’:
printme.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
printme.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
$

The <limits.h> header shall define various symbolic names.
The names represent various limits on resources that the
implementation imposes on applications.

about "-Wformat"
Check calls to printf and scanf, etc., to make sure that the
arguments supplied have types appropriate to the format
string specified, and that the conversions specified in the
format string make sense
 
Reference/Source:
The Definitive Guide to GCC Second Edition
William von Hagen
http://www.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html
http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
 

Leave a Reply

Your email address will not be published. Required fields are marked *