Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command Line Argument Counting

This is a simple C program that prints the number of command line argument passed to it:

#include <stdio.h>

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

When I give the input

file_name *

It prints 623 instead of 2 in my pc (operating system Windows 7). But it gives the correct output in other cases. Is * a reserved character for command line arguments? Note this program gives correct output for the following input:

file_name *Rafi

Output = 2

like image 209
Rafi Kamal Avatar asked Feb 14 '12 17:02

Rafi Kamal


1 Answers

On a Unix command line, the shell is responsible for handling wildcards. yourapp * will run yourapp, and pass the name of ALL of the non-hidden files in the current directory as arguments. In your case, that's 622 files (623 = 622 files + name of the program).

On Windows, applications are responsible for wildcard parsing, so argc is 2, 1 for the name of the program (argv[0]) and 1 for the wildcard (argv[1] = *);

like image 199
Marc B Avatar answered Oct 02 '22 20:10

Marc B