I am trying to write a simple programme to familiarise myself with command line argumentation. Currently, I am trying to specify that the programme ought only to accept a command line with two arguments. My attempt to specify that the programme take no more than two arguments worked fine:
#include <stdio.h>
int main(int argc, char* argv[])
{
for (int z = 1; z < argc; z++)
{
if (argc > 2)
{
printf("Nope\n");
return 0;
}
}
}
However, I then tried to specify that the programme should accept no less than two arguments:
#include <stdio.h>
int main(int argc, char* argv[])
{
for (int z = 1; z < argc; z++)
{
if (argc > 2)
{
printf("Nope\n");
return 0;
}
if (argc < 2)
{
printf("Nope\n");
return 0;
}
}
}
The programme has compiled fine, but when I try to run the programme with only one argument, I simply receive the message Segmentation fault
. I have tried googling this, but I am very new to programming and everything I can find on segmentation faults is beyond my basic knowledge. Anything which could help me fix my programme or help me understand what a segmentation fault is and why I have run afoul of it would be greatly appreciated.