getopt() function in C to parse command line arguments Last Updated : 10 Sep, 2018 Comments Improve Suggest changes Like Article Like Report The getopt() function is a builtin function in C and is used to parse command line arguments. Syntax: getopt(int argc, char *const argv[], const char *optstring) optstring is simply a list of characters, each representing a single character option. Return Value: The getopt() function returns different values: If the option takes a value, that value is pointer to the external variable optarg. '-1' if there are no more options to process. '?' when there is an unrecognized option and it stores into external variable optopt. If an option requires a value (such as -f in our example) and no value is given, getopt normally returns ?. By placing a colon as the first character of the options string, getopt returns: instead of ? when no value is given. Generally, the getopt() function is called from inside of a loop's conditional statement. The loop terminates when the getopt() function returns -1. A switch statement is then executed with the value returned by getopt() function. A second loop is used to process the remaining extra arguments that cannot be processed in the first loop. Below program illustrate the getopt() function in C: C // Program to illustrate the getopt() // function in C #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) { int opt; // put ':' in the starting of the // string so that program can //distinguish between '?' and ':' while((opt = getopt(argc, argv, “:if:lrx”)) != -1) { switch(opt) { case ‘i’: case ‘l’: case ‘r’: printf(“option: %c\n”, opt); break; case ‘f’: printf(“filename: %s\n”, optarg); break; case ‘:’: printf(“option needs a value\n”); break; case ‘?’: printf(“unknown option: %c\n”, optopt); break; } } // optind is for the extra arguments // which are not parsed for(; optind < argc; optind++){ printf(“extra arguments: %s\n”, argv[optind]); } return 0; } Output: Comment More infoAdvertise with us Next Article getopt() function in C to parse command line arguments S SrjSunny Follow Improve Article Tags : C Language Similar Reads Command Line Arguments in C The most important function of C is the main() function. It is mostly defined with a return type of int and without parameters.int main() { ... }We can also give command-line arguments in C. Command-line arguments are the values given after the name of the program in the command-line shell of Operat 4 min read How to Count Variable Numbers of Arguments in C? C supports variable numbers of arguments. But there is no language provided way for finding out total number of arguments passed. User has to handle this in one of the following ways: 1) By passing first argument as count of arguments. 2) By passing last argument as NULL (or 0). 3) Using some printf 2 min read Variable Length Argument in C Variable length argument is a feature that allows a function to receive any number of arguments. There are situations where we want a function to handle variable number of arguments according to requirement. 1) Sum of given numbers. 2) Minimum of given numbers. and many more. Variable number of argu 3 min read Write one line functions for strcat() and strcmp() Recursion can be used to do both tasks in one line. Below are one line implementations for stracat() and strcmp(). C /* my_strcat(dest, src) copies data of src to dest. To do so, it first reaches end of the string dest using recursive calls my_strcat(++dest, src). Once end of dest is reached, data i 2 min read User-Defined Function in C A user-defined function is a type of function in C language that is defined by the user himself to perform some specific task. It provides code reusability and modularity to our program. User-defined functions are different from built-in functions as their working is specified by the user and no hea 6 min read getchar Function in C C getchar is a standard library function that takes a single input character from standard input. The major difference between getchar and getc is that getc can take input from any no of input streams but getchar can take input from a single standard input stream. It is defined inside the <stdio. 3 min read main Function in C The main function is the entry point of a C program. It is a user-defined function where the execution of a program starts. Every C program must contain, and its return value typically indicates the success or failure of the program. In this article, we will learn more about the main function in C.E 5 min read Variable length arguments for Macros Like functions, we can also pass variable length arguments to macros. For this we will use the following preprocessor identifiers.To support variable length arguments in macro, we must include ellipses (...) in macro definition. There is also "__VA_ARGS__" preprocessing identifier which takes care o 2 min read vswscanf() Function in C/C++ The vfwscanf() function in C++ is used to read formatted data from wide string into variable argument list. It also reads wide character string from a wide string buffer. This function reads data from ws and stores them according to format into the locations pointed by the elements in the variable a 2 min read Like