snprintf VS itoa

use snprintf, it's the new more portable itoa.

itoa is not part of standard C, nor is it part of standard C++; but, a lot of compilers and associated libraries support it.

An example of a sprintf call.

char* buffer = ... allocate a buffer ...
int value = 4564;
sprintf(buffer, "%d", value);

An example of a snprintf call.

char buffer[10];
int value = 234452;
snprintf(buffer, 10, "%d", value);

the difference between sprintf and snprintf is that while sprintf formats in a printf style but writes the result to a string, snprintf formats in a printf style but guarantees no buffer overrun by writing up to a maximum number of characters to a string.

strcpy() => strncpy() 
sprintf() => snprintf() 
strcat() => strncat()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章