Wednesday, July 27, 2016

Why Does Xcode Give My Application A Command Line Argument?

Why Does Xcode Give My Application A Command Line Argument?

So, today I played around a little with my main function and decided to print out the argv and argc arguments in the main function.

This resulted in the following being printed out : 

Commandline argument amount : 1.

1. /Users/name/Library/Developer/Xcode/DerivedData/Playground-bdassylsrksorocyjtssfnwfbkpg/Build/Products/Debug/Playground

Input anything to continue.
a
Program ended with exit code: 0

After the '1.' it prints out the command line argument, my question is why does it have that command line argument? And what is it used for?

 

Here's the code I used for this little program : 

#include <iostream>

int main(int argc, const char* argv[]) {
    
    std::cout << "Commandline argument amount : " << argc << "." << std::endl;
    
    for (int i = 0; i < argc; i++)
    {
        std::cout << std::endl;
        std::cout << i + 1 << ". " << argv[i] << std::endl;
    }
    
    std::cout << std::endl;
    std::cout << "Input anything to continue." << std::endl;
    std::string inpt;
    std::cin >> inpt;
    
    return 0;
}

No comments:

Post a Comment