I have a question about the following code:
void testing(int idNumber)
{
char name[20];
snprintf(name, sizeof(name), "number_%d", idNumber);
}
The size of the char array name is 20, so if the idNumber is 111 it works, but how about the actual idNumber is 111111111111111111111111111111, how to determine how big the char array should be in order to keep the result of snprintf?
2^(sizeof(type) * 8)is the maximum (unsigned) value of the type. a one byte type (charorshort) allows for a 3 digit number, 2 bytes 5 (the minimal size of anint), 8, 10, 13, and so on (so the interval is 2 or 3 chars). Add the sign char and room for a terminating\0char, andsizeof(type) * 3 + 2should do the trick. If you are processingdoubles andfloats, add room for a decimal point, toodoublewith"%f"could need a buffer of size1 + DBL_MAX_10_EXP + 1 + 1 + 6 +1for the sign, DBL_MAX digits, ., fraction and'\0'. At least 47 and maybe 318charas on my platform. Your suggested formula works well for integers.long doubleis implemented as a quadrupal precision type (128 bit)long doubleuses one B.A.Buffer. ;-) (Maybe 4942char)