title too small, sprintf writes beyond title and writes to count , distorting its value.
Please note that title must be at least 8 bytes long: 3 for %.3d , 4 for .jpg and 1 for terminator \0 .
As Grijesh Chauhan points out, you can make sure that you never write outside of the allocated row size using snprintf , ie:
char title[8]; snprintf(title, sizeof(title), "%.3d.jpg", count);
smani source share