0

I just found out something rather strange here. It wasted me the better part of a day.

In MSVC, when an argument passed to the main program is abc&123, if one runs the program using the "Start Debugging" option, MSVC will pass the argument (one of argv[]) as "abc&123". But if one runs the program using "Start Without Debugging", MSVC will pass only "abc" and cut off whatever after the "&". What is the reason behind this?

user1596683
  • 131
  • 1
  • 8
  • 1
    When run without debugging, you have to put `abc&123` in quotes `"abc&123"`, to stop the command-line interpreter processing the `&` sign. – TonyK Jan 17 '15 at 08:19

1 Answers1

2

& is interpreted as a new command in your command line. nothing wrong with your code. OS interpret!

Create following code and test!

#include <iostream>

using namespace std;

int main(int argc,char *argv[])
{
    for(int i=0;i<argc;i++)
        cout<<"arg "<<i<<": "<<argv[i]<<endl;
    return 0;
}

test followings in command line:

appname aaa& bbb

appname "aaa& bbb"

The first line is interpreted as two separate commands:

appname aaa
bbb

while the second one is only one command:

appname "aaa& bbb"

This is the mechanism defined in shell and OS from back to MS-DOS. Quotations change the order of tokens similar to parenthesis in mathematics.

Update:

The debugger does pass the variable from different process. It knows that & is not referring to a new command. Start without debugging is more accurate. You may call it bug in the debugger.

Arashium
  • 325
  • 2
  • 9
  • So why is it different when debugging? – juanchopanza Jan 17 '15 at 08:24
  • No difference between running in command line or debugging. Ampersand separate the commands. This is problem of the other people too: http://stackoverflow.com/questions/1327431/how-do-i-escape-ampersands-in-batch-files – Arashium Jan 17 '15 at 08:29
  • The question is asking about this difference. You're not answering the question. – juanchopanza Jan 17 '15 at 08:30
  • The confusing part is the different behavior between "Start Debugging" and "Start Without Debugging", when the code is compiled EXACTLY the same, with the win32 debugging configuration. – user1596683 Jan 17 '15 at 08:30
  • 1
    I think people are looking for the words "shell interpretation" somewhere in this answer. – WhozCraig Jan 17 '15 at 08:31