Nov 04, 2016 Parsing C Command-Line Arguments.; 2 minutes to read +1; In this article. Microsoft Specific. Microsoft C/C startup code uses the following rules when interpreting arguments given on the operating system command line: Arguments are delimited by white space, which is. The command, when run from the prompt, works fine, but needs quoting for its parameter: C:'C:Program Files (x86)gsuninstgs.exe' 'C:Program Files (x86)gsgs8.63uninstal.txt' Without the quotes, it does not work. If I try to run this via CMD /C, I found no way to coerce CMD.EXE to pass a quoted string as parameter to the exe file.
Active2 years, 4 months ago
I'm trying to run a command from Perl, using Windows 7's CMD /C.The command, when run from the prompt, works fine, but needs quoting for its parameter:
Aug 07, 2009 Introduction Command-line parameters are passed to a program at runt-time by the operating system when the program is requested by another program, such as a command interpreter ('shell') like cmd.exe on Windows or bash on Linux and OS X. The user types a command and the shell calls the operating system to run the program. Command-line options for msiexec.exe for Windows Installer 3.0 and earlier. Provides a table showing options, parameters, and descriptions. Examples showing how to install products and other tasks.
Without the quotes, it does not work.
If I try to run this via CMD /C, I found no way to coerce CMD.EXE to pass a quoted string as parameter to the exe file. These do not work:
It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values.
What syntax should I use?
Laszlo ValkoLaszlo Valko49022 gold badges66 silver badges1212 bronze badges
1 Answer
Funny that cmd.exe actually contains the answer.
Here's a snipped from cmd /?
C Exe Parameters
So in your case it would be:
![C run exe with parameters C run exe with parameters](/uploads/1/2/4/8/124802196/399878106.png)
That said, it would also be possible to use the 8.3 short names and thus truncate Program Files to Progra~1 or Progra~2. Even more, you could use relative paths and first navigate to c:Program Files(x86) before executing your command. Your command would then become:
LPChipLPChip39.2k55 gold badges5757 silver badges9393 bronze badges
Not the answer you're looking for? Browse other questions tagged windowscommand-linecmd.exe or ask your own question.
- C Programming Tutorial
- C Programming useful Resources
- Selected Reading
It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values inside the code.
The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. Following is a simple example which checks if there is any argument supplied from the command line and take action accordingly −
![Exe Exe](/uploads/1/2/4/8/124802196/282464852.jpg)
When the above code is compiled and executed with single argument, it produces the following result.
When the above code is compiled and executed with a two arguments, it produces the following result.
When the above code is compiled and executed without passing any argument, it produces the following result.
It should be noted that argv[0] holds the name of the program itself and argv[1] is a pointer to the first command line argument supplied, and *argv[n] is the last argument. If no arguments are supplied, argc will be one, and if you pass one argument then argc is set at 2.
You pass all the command line arguments separated by a space, but if argument itself has a space then you can pass such arguments by putting them inside double quotes ' or single quotes '. Let us re-write above example once again where we will print program name and we also pass a command line argument by putting inside double quotes −
When the above code is compiled and executed with a single argument separated by space but inside double quotes, it produces the following result.