Handling command line flags in C/C++

前端 未结 7 887
星月不相逢
星月不相逢 2020-12-14 21:03

I am looking for a very simple explanation/tutorial on what flags are. I understand that flags work indicate a command what to do. For example:

rm -Rf test
<         


        
7条回答
  •  没有蜡笔的小新
    2020-12-14 21:27

    flags are arguments passed into the main entry point of the program. For example, in a C++ program you can have

    int main(int arc, char* argv[]){
    return 0;
    }
    

    your arc is the # of arguments passed in, and the pointer gives u the list of actual arguments. so for

    rm -Rf test
    

    argc would be 3, and the argv array would contain your arguments. Notice argc >= 1 because the program name itself counts (rm). -RF is your 2nd parameter and test is your third.

    So whenever you are typing commands in unix, you essentially are executing programs and passing them parameters that they operate on.

    If you are really REALLY interested in the unix OS, you should look up forks and how they work. This can get pretty confusing to a newcomer though, so only if you are really interested in OS and how programs are executed.

提交回复
热议问题