Question Details

No question body available.

Tags

c command-line-interface command-line-arguments

Answers (2)

February 8, 2026 Score: 3 Rep: 124,900 Quality: Medium Completeness: 50%

You'll always get at least 1 argument when main is called by the environment, the program name, in all environments I know of (but it's not mandated to be called with at least 1 argument and some embedded environments may call it with 0). To force it being called with 0 arguments, you could call main yourself:

int main(int argc, char* argv[]) { if (argc != 0) { return main(0, argv + argc); // main called with argc == 0 } // the rest of the function left unchanged
February 8, 2026 Score: 0 Rep: 760,312 Quality: Medium Completeness: 100%

Proof of "zero arguments possible" by counter-example

Here is a proof that "zero arguments" are possible by a (platform-specific) counter-example.

This code (I saved it in argv89.c) invokes a program specified on its command line with zero arguments:

#include 
#include 
#include 
#include 

int main(int argc, char argv) { char prog = "argc37"; if (argc > 2) fprintf(stderr, "Usage: %s [program]\n", argv[0]); else { if (argc == 2) prog = argv[1];

char
args[] = { NULL }; execv(prog, args); fprintf(stderr, "%s: failed to execute '%s' with zero arguments (%d: %s)\n", argv[0], prog, errno, strerror(errno)); } return 1; }

I modified your code to use argv (because my compilation options complain about unused arguments and I compile with -Werror), saving the source in so-7988-5166.c:

#include 

// When calling just, pass a different amount of arguments: just run 6 arg1 arg2 ...; int main(int argc, char* argv[]) { // This is impossible because there is always a command name if (argc