How to let lint check your format strings

Andries Brouwer aeb at mcvax.UUCP
Sun Mar 31 11:24:11 AEST 1985


In order to enable lint to check the correspondence between
the format specifications of printf-like functions (like %d, %ld, %s)
and the number and type of actual arguments one might do the following.
Replace each call
    sprintf(buf, "A %20s%*d", s, m, n);
by
    sprintf(buf, "A %20s%*d", procent_s(s), procent_d(m), procent_d(n));
The routines procent.. are declared in a file procent.c with a content like
    int procent_d(d) int d; { return(d); }
    long procent_D(D) long D; { return(D); }
    unsigned long procent_U(U) unsigned long U; { return(U); }
    char procent_c(c) char c; { return(c); }
    char *procent_s(s) char *s; { return(s); }
and in each source file one adds a first line
    #include "procent.h"
where procent.h is a file like
    extern int procent_d();
    extern long procent_D();
    extern unsigned long procent_U();
    extern char procent_c();
    extern char *procent_s();
Now lint can do the checking (with
    lint -options procent.c other_sources ...
).

I just posted a program (called printfck.c) doing this to net.sources.

(It works fine on the Hack & Quest sources, which is more than 20000 lines
 of code, but no guarantees are given. One has to be careful with the
 interaction with the C preprocessor. With lines like
    #define QUOTE "
 you'll run into obvious problems.)
        
        
[P.S. Yes, I know, for most C compilers the parentheses in  return (x)
 are superfluous.]



More information about the Comp.lang.c mailing list