int main()
{
char buf1[100], buf[100]="ddl";
sprintf(buf1, "log_name = '%.*s'", buf);
}
The above program is crashing. I am not able to understand why is this crashing.
As far as I know before the character makes printf to skip the format code and assign buf to next format code.
But here what is the significance of?
The format specifier
"%.*s"requires the number of characters to be written to be specified:In the posted code only
bufis provided so there are missing arguments, which is undefined behaviour (in this case a crash).Note, in this case,
"%s"would serve just as well asbufis null terminated (no requirment for a length unless all characters inbufmust not be copied).