+ 1
C - snprintf(char*, size, “%d”, int) Changes Value of Int to 10
Consider this short function: char* arr(char c, char* m) { int i; size_t size = strlen(m); char* arrs = malloc(size); i = (int) c; // outputs 100 if c == “d” printf(“%d”, i); snprintf(arrs, size, “%d”, i); // outputs 10 if c == “d” printf(“%s”, arrs); free(arrs); return (arrs); } Why is it that if the input (char c) is the letter “d”, it converts to ASCII int correctly (100), but using snprintf to convert char c to char* m outputs 10?
1 Odpowiedź
+ 1
Of course two minutes after I posted this, I realized I wasn’t accounting for the null terminator at the end of the string. Nevermind! Thanks anyway guys!