
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
getopt() function in C to parse command line arguments
The getopt() is one of the built-in C function that are used for taking the command line options. The syntax of this function is like below −
getopt(int argc, char *const argv[], const char *optstring)
The opstring is a list of characters. Each of them representing a single character option.
This function returns many values. These are like below −
- If the option takes a value, then that value will be pointed by optarg.
- It will return -1, when no more options to proces
- Returns ‘?’ to show that this is an unrecognized option, it stores it to optopt.
- Sometimes some options need some value, If the option is present but the values are not there, then also it will return ‘?’. We can use ‘:’ as the first character of the optstring, so in that time, it will return ‘:’ instead of ‘?’ if no value is given.
Example
#include <stdio.h> #include <unistd.h> main(int argc, char *argv[]) { int option; // put ':' at the starting of the string so compiler can distinguish between '?' and ':' while((option = getopt(argc, argv, ":if:lrx")) != -1){ //get option from the getopt() method switch(option){ //For option i, r, l, print that these are options case 'i': case 'l': case 'r': printf("Given Option: %c\n", option); break; case 'f': //here f is used for some file name printf("Given File: %s\n", optarg); break; case ':': printf("option needs a value\n"); break; case '?': //used for some unknown options printf("unknown option: %c\n", optopt); break; } } for(; optind < argc; optind++){ //when some extra arguments are passed printf("Given extra arguments: %s\n", argv[optind]); } }
Output
Given Option: i Given File: test_file.c Given Option: l Given Option: r Given extra arguments: hello
- Related Articles
- How to Parse Command Line Arguments in C++?
- Command Line arguments in C#
- Command line arguments in C/C++
- Command line arguments example in C
- Java command line arguments
- Command Line Arguments in Python
- Command line arguments in Java
- Command Line arguments in Lua
- Explain Java command line arguments.
- How to add command line arguments in Python?
- Command Line and Variable Arguments in Python?
- Command Line arguments in Java programming\n
- How command line arguments are passed in main method in C#?
- How do we access command line arguments in Python?
- How to pass command line arguments to a python Docker container?

Advertisements