Search code examples
c

How to Implement Option Recognition in C Programming for Linux


I'm programming in C at a Linux environment, and I'd like my program to recognize options like '-' or '--' for execution. I wonder how do programmers typically implement this?

In my case, I made the main function receive argc, argv arguments. Is that correct?

// headers

int main(int argc, char* argv[])
{
    // my codes
}

Solution

  • and I'd like my program to recognize options like '-' or '--' for execution. I wonder how do programmers typically implement this?

    Programmers have two ways:

    • write the option parser themselves
    • use the ready-made library. The most popular is getopt (more listed here)