How to Use the C-language getopt () function

Source: Internet
Author: User

This article introduces how to use the getopt () function in C language.
In Linux, executing an executable file using command lines may involve adding different parameters to it. For example:
./A. out-a1234-b432-c-d

The program will execute corresponding operations based on the read parameters. in C language, this function is generally implemented by the getopt () function and combined with the switch statement. First, let's look at the following code:
# Include <stdio. h>
# Include <unistd. h>
Int main (int argc, char * argv [])
{
Int ch;
Opterr = 0;
While (ch = getopt (argc, argv, "a: B: cde "))! =-1)
{
Printf ("optind: % d", optind );
Printf ("optarg: % s", optarg );
Printf ("ch: % c", ch );
Switch (ch)
{
Case:
Printf ("option a: % s", optarg );
Break;
Case B:
Printf ("option B: % s", optarg );
Break;
Case c:
Printf ("option c ");
Break;
Case d:
Printf ("option d ");
Break;
Case e:
Printf ("option e ");
Break;
Default:
Printf ("other option: % c", ch );
}
Printf ("optopt % c", optopt );
}
}

After compiling with gcc, run the preceding command on the terminal:
./A. out-a1234-b432-c-d
The output is as follows:
Optind: 2
Optarg: 1234
Ch:
Option a: 1234
Optopt
Optind: 3
Optarg: 432
Ch: B
Option B: 432
Optopt
Optind: 4
Optarg :( null)
Ch: c
Option c
Optopt
Optind: 5
Optarg :( null)
Ch: d
Option d
Optopt

To understand the functions of the getopt () function, you must first understand the usage of the main () function with parameters:
In main (int argc, char * argv []), argc is an integer, argv is a pointer array, and argc records the size of argv. In the above example.
Argc = 5;
Argv [0] =./a. out
Argv [1] =-a1234
Argv [2] =-b432
Argv [3] =-c
Argv [4] =-d
The prototype of the getopt () function is getopt (int argc, char * const argv [], const char * optstring ).
Here, argc and argv generally pass in the two parameters of the main function as they are.
Optstring is a set of optional strings. For example, in this example, "a: B: cde" indicates that it can have,-a,-B,-c,-d, -e parameters.
":" Indicates that this option must contain additional parameters. The global variable optarg points to this additional parameter. ":" this parameter is optional to identify this additional parameter (which may not be supported by some Uinx ":: ").
The global variable optind indicates the location of the next parameter to be read in argv.
If getopt () fails to find the correct parameter, an error message is printed and the global variable optopt is set "? "Character.
If you do not want getopt () to print the error message, set the global variable opterr to 0.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.