My initial idea for the credit problem was to convert the long from get_long into a char array, in order to obtain something iterable to get each digit for Luhn's algo.
I recognize that this could be achieved using modulo instead without having to convert the long.
However, I am wondering why this code does not result in a buffer overflow.
#include <cs50.h>
#include <stdio.h>
#define DIGIT 4 /* max digit length for any credit card */
int main (void) {
// prompt user for number
long n = get_long("Number: ");
// verify Luhn's checksum algo
// convert long to string for enumerating char array
char str[DIGIT];
sprintf(str, "%lu", n);
int len = snprintf(NULL, 0, "%s", str);
printf("%d", len);
printf("%s", str);
}
Originally, the DIGIT constant was set to 16, but I was testing and now am confused as to why sprintf does not result in a buffer overflow when I input a number with more than 4 digits, ie
Number: 12345
5
12345