Skip to content

Document exceptions to the rules for parsing C arguments #2585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions docs/c-language/parsing-c-command-line-arguments.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
title: "Parsing C Command-Line Arguments"
ms.date: "11/04/2016"
helpviewer_keywords: ["quotation marks, command-line arguments", "double quotation marks", "command line, parsing", "parsing, command-line arguments", "startup code, parsing command-line arguments"]
description: "Learn how the Microsoft C runtime startup code interprets command-line arguments to create the argv and argc parameters."
ms.date: 11/09/2020
helpviewer_keywords: ["quotation marks, command-line arguments", "double quotation marks", "double quote marks", "command line, parsing", "parsing, command-line arguments", "startup code, parsing command-line arguments"]
ms.assetid: ffce8037-2811-45c4-8db4-1ed787859c80
---
# Parsing C Command-Line Arguments
Expand All @@ -12,15 +13,17 @@ Microsoft C startup code uses the following rules when interpreting arguments gi

- Arguments are delimited by white space, which is either a space or a tab.

- A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument. Note that the caret (**^**) is not recognized as an escape character or delimiter.
- The first argument (`argv[0]`) is treated specially. It represents the program name. Because it must be a valid pathname, parts surrounded by double quote marks (**`"`**) are allowed. The double quote marks aren't included in the `argv[0]` output. The parts surrounded by double quote marks prevent interpretation of a space or tab character as the end of the argument. The later rules in this list don't apply.

- A double quotation mark preceded by a backslash, **\\"**, is interpreted as a literal double quotation mark (**"**).
- A string surrounded by double quote marks is interpreted as a single argument, whether or not it contains white space. A quoted string can be embedded in an argument. The caret (**`^`**) isn't recognized as an escape character or delimiter. Within a quoted string, a pair of double quote marks is interpreted as a single escaped double quote mark. If the command line ends before a closing double quote mark is found, then all the characters read so far are output as the last argument.

- Backslashes are interpreted literally, unless they immediately precede a double quotation mark.
- A double quote mark preceded by a backslash (**`\"`**) is interpreted as a literal double quote mark (**`"`**).

- If an even number of backslashes is followed by a double quotation mark, then one backslash (**\\**) is placed in the `argv` array for every pair of backslashes (**\\\\**), and the double quotation mark (**"**) is interpreted as a string delimiter.
- Backslashes are interpreted literally, unless they immediately precede a double quote mark.

- If an odd number of backslashes is followed by a double quotation mark, then one backslash (**\\**) is placed in the `argv` array for every pair of backslashes (**\\\\**) and the double quotation mark is interpreted as an escape sequence by the remaining backslash, causing a literal double quotation mark (**"**) to be placed in `argv`.
- If an even number of backslashes is followed by a double quote mark, then one backslash (**`\`**) is placed in the `argv` array for every pair of backslashes (**`\\`**), and the double quote mark (**`"`**) is interpreted as a string delimiter.

- If an odd number of backslashes is followed by a double quote mark, then one backslash (**`\`**) is placed in the `argv` array for every pair of backslashes (**`\\`**). The double quote mark is interpreted as an escape sequence by the remaining backslash, causing a literal double quote mark (**`"`**) to be placed in `argv`.

This list illustrates the rules above by showing the interpreted result passed to `argv` for several examples of command-line arguments. The output listed in the second, third, and fourth columns is from the ARGS.C program that follows the list.

Expand All @@ -31,13 +34,13 @@ This list illustrates the rules above by showing the interpreted result passed t
|`a\\\b d"e f"g h`|`a\\\b`|`de fg`|`h`|
|`a\\\"b c d`|`a\"b`|`c`|`d`|
|`a\\\\"b c" d e`|`a\\b c`|`d`|`e`|
|`a"b"" c d`|`ab" c d`|||

## Example

### Code

```c
// Parsing_C_Commandline_args.c
// ARGS.C illustrates the following variables used for accessing
// command-line arguments and environment variables:
// argc argv envp
Expand Down