Let len be the length of the formatted data string (not including the terminating null). len and count are in bytes for _snprintf, wide characters for _snwprintf. If len < count, then len characters are stored in buffer, a null-terminator is appended, and len is returned. If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned. If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned. "如果字符串很长,超过了buffer的size,是否会截断后还是在buffer末尾添'\0'?" If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned. 回答完毕。 以上是vc里ok,linux下的snprintf截断后会添加'\0',这个要注意。
int a = 5; char str[19] = {0}; snprintf(str, 19, "%018p", &a); printf("%s\n", str); printf("%018p", &a);