Counting Command Line Arguments

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

#include <stdio.h>

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

When i give input

file_name *

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

file_name *Rafi

Output = 2

+5
source share
4 answers

Unix . yourapp * yourapp . 622 (623 = 622 + ).

Windows , argc 2, 1 (argv [0]) 1 (argv [1] = *);

+10

* ( * nixes, Windows), literal * .

+3

, " " "", * argv.

Unix ( ) C.

Windows ( , - Unix- , ​​ Cygwin). globbing C , / :

  • Microsoft, C , argc 2 . , setargv.obj ( wsetargv.obj, Unicode), , , Unix. setargv.obj MSVC , , . , Windows .

  • MinGW/GCC, C main() ( , MinGW 4.6.1 - , MinGW ). , MinGW . globing MinGW :

    • _CRT_glob 0:

      int _CRT_glob = 0;
      
    • lib/CRT_noglob.o ( , - ):

      gcc c:/mingw/lib/CRT_noglob.o main.o -o main.exe
      
+2

The problem is that the shell expands *to all file names (which do not start with .) in the current directory. It's all about the shell and has very little to do with C.

The value argcincludes 1 for the program's own name, plus one for each argument passed by the shell.

Try:

filename *
filename '*'

The first will give you 623 (give or take - but it's time you cleared this directory!). The second will give you 2.

+1
source

All Articles