Print part of a string with printf

This example program shows how to print only some of the bytes of a C string using printf and the * formatting statement.

#include <stdio.h>

int main ()
    const char * test = "This is a fox.";
    printf ("No qualification: '%s'\n", test);
    printf ("Only first four bytes: '%.*s'\n", 4, test);
    printf ("Get some middle bytes: '%.*s'\n", 3, test + 10);
    return 0;


It produces the following output:

No qualification: 'This is a fox.'
Only first four bytes: 'This'
Get some middle bytes: 'fox'

Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer