TOC | Prev | Next

printf

The printf family of functions do no checking of their arguments.

hello-printf.c

#include <stdio.h>
#include <stdlib.h>

int main( void ) {
    const char conference[] = "OSCON";
    const int year = 2009;

    printf( "Hello, %s %d!\n", conference, year );

    printf( "Hello, %s %d!\n", year, conference );

    return 0;
}

Compiling this with -Wall gives ample warnings of our mistake:

$ make
gcc -Wall -pedantic hello-printf.c -o hello-printf
hello-printf.c: In function 'main':
hello-printf.c:10: warning: format '%s' expects type 'char *', but argument 2 has type 'int'
hello-printf.c:10: warning: format '%d' expects type 'int', but argument 3 has type 'const char *'

However, those are just warnings, and gcc still creates an executable.

Running it is tragic.

$ ./hello-printf
Hello, OSCON 2009!
Bus error
TOC | Prev | Next