I am performing sprintf on a char array of size 1 and the code works fine. i.e strlen returns 1 even though it may not be null terminated. To my understanding sprintf will do null termination, however in this case it does not have enough space. Can someone explain the reason it works?
#include <stdio.h>
#include <string.h>
#define STATUS "1"
int main(void){
char *data = malloc(1);
sprintf(data, "%s", STATUS);
printf(">%s<\n", data);
printf(">%d<\n", strlen(data));
}
Output
>1<
>1<