Credit card verification: string conversion most optimal? Below is some code which verifies a credit card number using the checksum as well as check if number of digits are appropriate as well if digits start with right numbers. I am not sure if converting the double into a string was the best bet. I wasn't going to at first but had trouble figuring out the modulo math to get every second digit without knowing the length of the double (# of digits).

Also, in my use of strtok(), what should I be doing with the balance of the string after the delimiter? Is that hanging out in memory somewhere?

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

int main(void)
{
    double cardnumber;
    printf("Give me a number: \n");
    scanf("%lf", &amp;cardnumber);

    if(cardnumber &lt; 1000000000000 || cardnumber &gt; 10000000000000000)
    {
        printf("INVALID\n");
        return 0;
    }
    if(cardnumber &lt; 100000000000000 &amp;&amp; cardnumber &gt; 9999999999999)
    {
        printf("INVALID\n");
        return 0;   
    }

    char creditcard[17];
    sprintf(creditcard, "%f", cardnumber);
    char* ptr_cc;
    ptr_cc = strtok(creditcard,".");
    int card_size = strlen(ptr_cc);
    int sum = 0;
    for(int i = 1; i &lt; card_size; i+=2)
    {
        int x = creditcard[card_size-1-i] - '0';
        int prod = 2 * x;
        if(prod&gt;=10)
        {
            prod = prod%10 + prod/prod%10;
        }
        sum += prod;      
    }
    for(int i = 0; i &lt; card_size; i+=2)
    {
        int x = creditcard[card_size-1-i] - '0';
        sum += x;      
    }
    if(sum%10 != 0)
    {
        printf("INVALID\n");
        return 0; 
    }
    else if(creditcard[0] == '4')
    {
        printf("VISA\n");
        return 0; 
    }
    else if(creditcard[0] == '3' &amp;&amp; (creditcard[1] == '7' || creditcard[1] =='4'))
    {
        printf("AMEX\n");
        return 0; 
    }
    else if(creditcard[0] == '5' &amp;&amp; (creditcard[1] &gt;='1' &amp;&amp; creditcard[1] &lt;='5'))
    {
        printf("MASTERCARD\n");
        return 0; 
    }
    else
    {
        printf("INVALID\n");
        return 0;    
    }         
}
As @Keith says, credit card numbers should be treated as strings, not numbers, and definitely not floating-point numbers.  If you want to ensure that the input contains only digits (and maybe spaces), use strspn().  Squeeze out any spaces, then validate the length using strlen().

The Luhn checksum check should be in its own function.  The loop indexes would be more natural counting down, I think, since you are taking every other digit starting from the right.

int is_valid_luhn(const char *creditcard)
{
    int card_size = strlen(creditcard);
    int sum = 0;
    for(int i = card_size - 2; i &gt;= 0; i -= 2)
    {
        int digit = creditcard[i] - '0';
        int prod = 2 * digit;
        sum += prod / 10 + prod % 10;      /* No special case needed */
    }
    for(int i = card_size - 1; i &gt;= 0; i -= 2)
    {
        sum += creditcard[i] - '0';
    }
    return sum % 10 == 0;
}