How to check if there is NO command line arguments in C

819 views Asked by At

I am creating simple calculator. I give arguments to be calculated as command line arguments.

How do check if user gave no arguments at all?

I Tried this, but i get segmentation fault:

int main(int argc, char* argv[]){
    if(argc == 0){
        printf("No arguments were given");
    return 0;
}
1

There are 1 answers

0
chux - Reinstate Monica On

argc is the count of the program name and its arguments.

When argc == 1, there is only the program name and no additional arguments.

It is possible for argc == 0 as that implies even the program name is not passed in.

Test against 1

if (argc <= 1) {
  printf("No arguments were given.\n");
}

i get segmentation fault:

This is certainly due to unposted parts of OP's code.