A
Alf P. Steinbach
* Simon Biber:
Very off-topic I guess, but the above is not quite correct. In Windows
the argument line is passed to the process as-is, and is then split into
separate 'main' arguments by the C run time library. There is a command
line parsing (splitting-into-arguments) API function that the run time
library can use, but usually it's not used, and in one case, an article
published in DDJ (IIRC) by Brian Kernighan (IIRC), the correct working
of an example relied, without mention, on a non-conventional parsing
(expanding wildcards) that is supported by some compilers by applying
suitable compiler or linker switches, and this caused some debate.
I have a program which reads in 3 filenames from the command line
prog filename1 filename2 filename3
However, it doesn't work when one of the filenames has spaces in it
(due to a directory name with a space in it) because that filename gets
split into 2.
I tried
prog "/blah/blah 2/filename1" "filename2" "filename3"
and it still splits on the space. (It included the " chars too).
That's strange; what you have given above works on most platforms.
Strictly, the way in which program arguments are supplied is not defined
by the C standard. The C standard does not require that there is such a
thing as a command line, or that arguments are separated by spaces.
<OT>
As Rouben said, on Unix platforms it is up to the shell to parse the
command line and separate out the individual arguments. Unix shells
typically support double quotes, single quotes and also backspace
escaping of spaces.
On the Windows command line, double quotes are supported and will result
in the correct escaping behaviour:
C:\docs\prog\c>testargs "hello world" "foo"
argc = 3
argv[0] = testargs
argv[1] = hello world
argv[2] = foo
But single quotes will not be recognised as anything special:
C:\docs\prog\c>testargs 'hello world' 'foo'
argc = 4
argv[0] = testargs
argv[1] = 'hello
argv[2] = world'
argv[3] = 'foo'
</OT>
Very off-topic I guess, but the above is not quite correct. In Windows
the argument line is passed to the process as-is, and is then split into
separate 'main' arguments by the C run time library. There is a command
line parsing (splitting-into-arguments) API function that the run time
library can use, but usually it's not used, and in one case, an article
published in DDJ (IIRC) by Brian Kernighan (IIRC), the correct working
of an example relied, without mention, on a non-conventional parsing
(expanding wildcards) that is supported by some compilers by applying
suitable compiler or linker switches, and this caused some debate.