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
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
The Definitive Guide to GCC Second Edition
William von Hagen

Leave a Reply

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