Skip to content

Commit 98a5c65

Browse files
author
Colin Robertson
authored
Clarify argument handling. Add description.
1 parent e5bdbb7 commit 98a5c65

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

docs/c-language/parsing-c-command-line-arguments.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
title: "Parsing C Command-Line Arguments"
3-
ms.date: "11/04/2016"
4-
helpviewer_keywords: ["quotation marks, command-line arguments", "double quotation marks", "command line, parsing", "parsing, command-line arguments", "startup code, parsing command-line arguments"]
3+
description: "Learn how the Microsoft C runtime startup code interprets command-line arguments to create the argv and argc parameters."
4+
ms.date: 11/09/2020
5+
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"]
56
ms.assetid: ffce8037-2811-45c4-8db4-1ed787859c80
67
---
78
# Parsing C Command-Line Arguments
@@ -12,17 +13,17 @@ Microsoft C startup code uses the following rules when interpreting arguments gi
1213

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

15-
- 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.
16+
- 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.
1617

17-
- A double quotation mark preceded by a backslash, **\\"**, is interpreted as a literal double quotation mark (**"**).
18+
- 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.
1819

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

21-
- 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.
22+
- Backslashes are interpreted literally, unless they immediately precede a double quote mark.
2223

23-
- 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`.
24+
- 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.
2425

25-
- A double quotation mark appearing after a closing double quotation mark, **"*""**, will be interpreted as a literal double quotation mark (**\*"**). It might also (depending on the target program) delimit the start of a new string region.
26+
- 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`.
2627

2728
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.
2829

@@ -33,16 +34,13 @@ This list illustrates the rules above by showing the interpreted result passed t
3334
|`a\\\b d"e f"g h`|`a\\\b`|`de fg`|`h`|
3435
|`a\\\"b c d`|`a\"b`|`c`|`d`|
3536
|`a\\\\"b c" d e`|`a\\b c`|`d`|`e`|
36-
|`a"b"" c d`|`ab"`|`c`|`d`|
37-
38-
Note that the command itself (**argv[0]**) may use different parsing rules, and often does not treat a backslash as an escape character.
37+
|`a"b"" c d`|`ab" c d`|||
3938

4039
## Example
4140

4241
### Code
4342

4443
```c
45-
// Parsing_C_Commandline_args.c
4644
// ARGS.C illustrates the following variables used for accessing
4745
// command-line arguments and environment variables:
4846
// argc argv envp

0 commit comments

Comments
 (0)