+ 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 Answer
+ 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!