Search code examples
cprogram-entry-pointargc

How exactly does argc work when I don't use quotation marks?


My code is very simple :

#include <stdio.h>

int main(int argc, char **argv)
{
    printf("%d\n", argc);
    return 0;
}

It prints 17 after I input

./a.out 1 2 3 + *

Shouldn't it print 6 instead? The program's behavior is as expected if I put '' around every argument except ./a.out. This is strange, because I can use some programs with flags without using quotation marks('', "").


Solution

  • If you don't quote or escape *, it's expanded as a wildcard by the shell when forming the program arguments. It's replaced with all the names in the current directory (except the ones that begin with .).

    You must have had 12 files and subdirectories in your directory when you ran this.