Taking 2 command line inputs and verifying that they are integers, within the correct range of values I'm taking 2 command line inputs, which have to be integers between 2 and 1,000,000. These two values are then passed into the getPrimes function, which is just a Sieve of Eratosthenes algorithm, printing all prime numbers within the range specified by the two inputs. The two inputs, n and k, must be positive integers, where n > k. How can I better optimise my approach to collecting these inputs and verifying them, to decrease execution time?

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;math.h&gt;
#include &lt;ctype.h&gt;
#define MAXVALUE 1000000

int main(int argc, char *argv[]){

    if (argc != 3){
        printf("Warning: Check number of arguments.\n");
        return 1;
    }

    int i, j;
    for (j = 1; j &lt;= 2; j++){
        for (i = 0; argv[j][i] != '\0'; i++){
            if (!isdigit(argv[j][i])) {
                printf("Warning: Please enter a valid integer.\n");
                return 1;
            }
        }
    }

    int n = atoi(argv[2]), k = atoi(argv[1]);

    if (n &gt; MAXVALUE || k == 1 || k &gt;= n){
        printf("Warning: Bad input.\n");
        return 1;
    } 

    // getPrimes(n, k);

    return 0;

}
This is unrelated to performance, but it may still be helpful in general:


You should print those errors to stderr via fprintf():

fprintf(stderr, "Warning: Check number of arguments.\n");




fprintf(stderr, "Warning: Please enter a valid integer.\n");




fprintf(stderr, "Warning: Bad input.\n");

Are you using a pre-C99 version of C?  If not, you should be initializing the loop counter variables within the loop conditions, not outside of them.
It's common to provide the types of expected arguments (starting with whatever gets printed from argv[0]) if this check fails. You could also specify the number of expected arguments so that the user isn't left guessing, in case the objective isn't already clear.

fprintf(stderr, "Warning: Check number of arguments (must be 2).\n");

You shouldn't try to do everything in main(), and it doesn't look like those checks after the arguments check belong there.

Instead, consider providing separate functions for both that get called from main(). They can return a boolean value and, if it's an erroneous one, the program can then terminate from main().
If I'm not mistaken, the for loop is supposed to check both argv[1] and argv[2] for valid data types.

If so, you could either:


Do two separate checks without a loop (to maybe make it a little more cleaner)
If you may consider expanding it for additional arguments, use argc as part of the outer loop sentinel value instead of hard-coding the 2 (which could be seen as a "magic number" instead anyway)