简体   繁体   中英

c++ main function, argc value is weird if command-line arguments contain *

A very simple piece of C++ code like this:

int main(int argc, char **argv){
    std::cout << "argc: " << argc << std::endl;
}

Compiled with g++ -o hello hello.cpp

  • when run with ./hello u , output is argc: 2 ;
  • when run with ./hello u + , output is argc: 3 ;
  • when run with ./hello u * , output is argc: 26 , why 26 ?

Shell expansion. * is expanded by the shell into all files in the current directory, of which there appear to be 24, and passes them as individual arguments to your program.

Since this looks like a call from a UNIX shell, use

./hello u \*

or

./hello u '*'

You need to pass what the shell interprets as special characters in ' ' .

So the proper command line invoke, should have been ./hello u '*'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM