function(char* name)
{
char sql[50];
sprintf(sql, "select %s;", name);
}
What's the best way to make sure only 50 chars of name are copied to sql in the case name is larger than what sql can hold? (sprintf with a N 开发者_开发技巧parameter?)
Thank You.
There is snprintf
, which also takes a size parameter:
int snprintf(char *str, size_t size, const char *format, ...);
snprintf
, although it does not null terminate if you print N characters.
Most compilers have an snprintf() function.
You want snprintf().
int snprintf(char *str, size_t size, const char *format, ...);
精彩评论