How do I pass an arguement and print that amount of numbers in my program? So far I have found out that if I change count to a lower number than argc, the amount of numbers increases. However, I want it to print x amount of numbers on whatever I type in the command line like 10. Thanks
int random( int limi, int lims);
int main (int argc, char *argv[])
{
int count;
srand(time(NULL));
int p;
int input;
int i = 0;
if (argc > 1)
{
for ( count = 1; count < argc; count++)
{
p = random(-10000,10000);
printf(" %d\n",p );
}
}
else
{
printf("The command had no other arguments.\n");
}
return 0;
}
int random( int limi, int lims )
{
return ( rand() % (lims - limi) - lims);
}
The arguments to your application are passed in as an array of strings in argv[]. To convert the first argument to an integer, parse it using atoi(), strtol() or sscanf():
if (argc > 1)
{
int amount = atoi(argv[1]);
for ( count = 1; count < amount; count++)
{
p = random(-10000,10000);
printf(" %d\n",p );
}
}
The arguments start from argv[1]. argv[0] will be the name of your program.