I have a simple code which uses sprintf
#include <stdio.h>
int main()
{
char str_src [1024]={"Hello"};
sprintf(str_src,"%s%s",str_src,"hiiiiiiiiiii");
printf("result = %s",str_src);
}
When i compile i get correct result :
result = Hellohiiiiiiiiiii
But since sprintf is unsecure, i decided to change this to snprintf. I thought it would be really simple. I changed sprintf to snprintf like below
snprintf(str_src,1024,"%s%s",str_src,"hiiiiiiiiiii");
Now If i compile and run the code, i get different result
result = hiiiiiiiiiii
I face this problem if i use str_src as 4th parameter (as a value to %s). Its suprising why the behavior of snprintf is different than sprintf?