C Programming

Getopt 3 C Function

The getopt() function is a built-in function in C programming, and we use it for parsing command-line arguments in the C programming language. When we want to parse the command-line arguments, we utilize this “getopt()” function there. This “getopt()” function can be used as a conditional statement inside the loop. When the “getopt()” method returns -1, the loop is over. With the value given by the “getopt()” function, a switch statement is then put into action. The remaining additional arguments that cannot be handled in the first loop are executed in a second loop.

In this article, we will utilize this “getopt()” function in our C program and also explain the working of this “getopt()” function.

Syntax of the getopt 3 Function in C Language

 

We have an integer “int” named “argc”. The “char” is named “const argv [ ]”, which is an array, and also the “const char” named “optstring”. This optstring is only a series of characters, each of which stands for a single option.

Example # 1

In this article, these given examples are executed on Ubuntu 20.04. Therefore, to execute this, Ubuntu 20.04 have the GCC installed. After installing it, we’ll write some code in our C programming language to show you the working of the “getopt()” function. Here, we are writing codes in the text editor of Ubuntu 20.04. First, we first add several header files to the code provided below, such as “<stdio. h>,” which is used for printing on the screen.

Then, we utilize the “<unistd.h>” header file, which is used for providing access to the “POSIX” OS API. Now, we have a “main()” function and pass parameters to this main() function as “argc” of the “int” data type and “argv []” array of “char” data type. Inside this “main()” function, we have “opt” and it is also an “int” data type. After this, we have a “while” loop and we utilize the “getopt()” function as the condition of this loop. We put the “opt” here and initialize it with the “getopt()” function. And we put the condition here that this “getopt()” function is not equal to “-1”.

Then, we have a “switch” statement in which we pass “opt” as the parameter. Then, we have different cases. It checks these conditions if the first case is not satisfied, then it moves to the next case. If the case is true and satisfied, then the statement inside this will be executed and we have the “break” statement. We have the “printf” statement inside all these cases of the switch statement. In the case of “l”, “h”, and “m”, we put the “printf” statement which prints the “character” on the output screen. In the case of “f”, it prints the “filename”.

We put the “:” at the start of the string so that it can easily distinguish among “:” and “?”. If there are no more alternatives to the process, the result is “-1”. The “?” is displayed when an option is not recognized and is stored in the external variable optopt. The getopt typically returns if an option expects a value (like -f in our example) but none is supplied. We also have the “for” loop which is here for the extra arguments that are not parsed. In the “if” statement, we have the “printf” statement which is used for printing the given statement on the terminal screen.


 

Here is the end result of the overhead script, and you will see how this function works here. We get this output by typing the commands, which are shown in the following screenshot:

Example # 2

Here, we have another example in which we utilize the header files. The header files we use here are “stdio. h” and “getopt. h”. For both reading and writing characters to files, the “stdio. h” header file is utilized. We have the “int” type “main” function where we pass two parameters as “int” and “char” data types.

Then, we open the curly braces of the “main” function and declare and initialize the “option-index” as the “int” data type. The “char” data type is named as “user_name” and initialized with “NULL”. After this, we put the “while” loop and we have assigned the “getopt ()” function to this “option_index”. Below this, we have the switch statement and pass this “option_index” to this switch statement and different cases of this switch statement are here. In the first “u” case, we have two “printf” statements, and we put the “break” statement here. In the case of “g”, we put only one “printf” statement, and then we have “break”. We must “break” after each case of the switch statement.

Now, we also use the “default”. This will execute only when all the conditions are not satisfied. If all cases are false, then this “default” statement will be executed. We also put the “printf” statement in this “default” statement and then close all the brackets.


Below this, the output of the second example. The previous code gives the output which is shown in the following screenshot:

Conclusion

The major focus of this article is to show you how to utilize the C programming function “getopt()”. In this article, we’ve looked at the “getopt()” function and covered its purpose, justification, usage, and results. This article has already covered the “getopt()” function in C. In C programming, the “getopt()” function is used for parsing command-line arguments in C, as we have already discussed. Here, we’ve looked at several examples. The output from each example in this article has also been displayed, along with the C programming codes for using the “getopt()” function. After carefully reading this article, we are confident that you will have a firm interpretation of the “getopt()”.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.