Here you are.
#include <stdio.h>
int main(void)
{
char *s = "abcdef";
size_t pos = 1;
int n = 3;
printf( "%.*s\n", n, s + pos );
return 0;
}
The program output is
bcd
Or another example
#include <stdio.h>
#include <string.h>
int main(void)
{
char *s = "abcdef";
for ( int i = 0, n = ( int )strlen( s ); i < n; i++ )
{
printf( "%.*s\n", i + 1, s );
}
return 0;
}
The program output is
a
ab
abc
abcd
abcde
abcdef
You can write a generic function that can output a sub-string in any stream for example in an opened file.
#include <stdio.h>
#include <string.h>
FILE * print_substring( const char *s, size_t pos, size_t n, FILE * fp )
{
size_t len = strlen( s );
if ( pos < len )
{
if ( len - pos < n ) n = len - pos;
fprintf( fp, "%.*s", ( int )n, s + pos );
}
return fp;
}
int main(void)
{
char *s = "abcdef";
for ( int i = 0, n = ( int )strlen( s ); i < n; i++ )
{
putc( '\n', print_substring( s, 0, i + 1, stdout ) );
}
return 0;
}
The program output is
a
ab
abc
abcd
abcde
abcdef