String overflow behaviour
I wanted to check out what strlen did if the string it was acting upon overflowed...
Code:
#include <stdio.h>
#include <string.h>
int
main()
{
char str[10];
sprintf( str, "four" );
printf( "\n%s - %d", str, strlen( str ));
strcat( str, "four" );
printf( "\n%s - %d", str, strlen( str ));
strcat( str, "four" );
printf( "\n%s - %d", str, strlen( str ));
printf( "\n" );
return 0;
}
gave this output:
four - 4
fourfour - 8
fourfourfour - 12
Is this behaviour predictable i.e. can I check that a string has not overflowed by using strlen to ensure that the length of the string is less than the space originally allocated to it?
Thanks.