There’s no standard function for this, but you can define
bool prefix(const char *pre, const char *str)
{
return strncmp(pre, str, strlen(pre)) == 0;
}
We don’t have to worry about str being shorter than pre because according to the C standard (7.21.4.4/2):
The
strncmpfunction compares not more thanncharacters (characters that follow a null character are not compared) from the array pointed to bys1to the array pointed to bys2.”