Command Line Arguments in C Language with Examples
Command-line arguments are used to pass input values to a C program when it is executed from a command-line interface, such as Windows Command Prompt or a Linux terminal. These arguments allow users to control program behaviour without requesting input through the program itself.
सी लैंग्वेज में कमांड लाइन आर्गुमेंट का उपयोग प्रोग्राम को चलाते समय Command Prompt या Linux Terminal से इनपुट देने के लिए किया जाता है। इनकी सहायता से यूजर प्रोग्राम के अंदर इनपुट लिए बिना ही उसके कार्य को नियंत्रित कर सकता है।
Command-line arguments are passed to the main() function. The main() function can receive them through two parameters: argc and argv.
कमांड लाइन आर्गुमेंट main() फंक्शन को प्रदान किए जाते हैं। main() फंक्शन इन्हें दो पैरामीटर argc और argv की सहायता से प्राप्त करता है।
Syntax of Command Line Arguments
int main(int argc, char *argv[]){// Statementsreturn 0;}
Here, argc and argv are the two parameters used to receive command-line arguments.
यहाँ argc और argv दो पैरामीटर हैं जिनका उपयोग कमांड लाइन आर्गुमेंट प्राप्त करने के लिए किया जाता है।
1. argc (Argument Count)
argc is an integer parameter that stores the total number of command-line arguments, including the program name. In a hosted C environment, its value is at least 1.
argc एक Integer पैरामीटर है जो कमांड लाइन आर्गुमेंट की कुल संख्या को दर्शाता है। इसमें प्रोग्राम का नाम भी शामिल होता है। सामान्य C प्रोग्राम में इसका मान कम से कम 1 होता है।
2. argv (Argument Vector)
argv is an array of pointers to characters. Each element points to a null-terminated string containing a command-line argument.
argv Character Pointers का एक Array है। इसका प्रत्येक Element एक Null-Terminated String को पॉइंट करता है, जिसमें कमांड लाइन से प्राप्त एक आर्गुमेंट होता है।
argv[0]represents the program name or its invocation name.argv[1]represents the first command-line argument.argv[2]represents the second command-line argument.argv[argc - 1]represents the last command-line argument, when arguments are present.argv[argc]is guaranteed to beNULL.argv[0]प्रोग्राम के नाम या उसे चलाने के लिए उपयोग किए गए नाम को दर्शाता है।argv[1]पहला कमांड लाइन आर्गुमेंट दर्शाता है।argv[2]दूसरा कमांड लाइन आर्गुमेंट दर्शाता है।argv[argc - 1]अंतिम कमांड लाइन आर्गुमेंट दर्शाता है, यदि आर्गुमेंट उपलब्ध हैं।argv[argc]का मान हमेशाNULLहोता है।
Example 1: C Program to Display Command Line Arguments
The following program displays the program name and all command-line arguments supplied by the user.
निम्न प्रोग्राम यूजर द्वारा दिए गए प्रोग्राम नाम और सभी कमांड लाइन आर्गुमेंट को स्क्रीन पर प्रदर्शित करता है।
#include <stdio.h>int main(int argc, char *argv[]){int i;printf("Program Name: %s\n", argv[0]);printf("Total Arguments: %d\n\n", argc);for (i = 1; i < argc; i++){printf("Argument %d: %s\n", i, argv[i]);}return 0;}
Compile and Execute
Save the program as demo.c. Compile it using GCC:
gcc demo.c -o demoRun the program by providing command-line arguments:
./demo Ritesh 25 COn Windows, you can run:
demo.exe Ritesh 25 COutput
Program Name: ./demoTotal Arguments: 4Argument 1: RiteshArgument 2: 25Argument 3: C
The output shows that argc contains 4 because it counts the program name and the three supplied arguments.
इस आउटपुट में argc का मान 4 है क्योंकि इसमें प्रोग्राम के नाम और दिए गए तीन आर्गुमेंट की कुल संख्या शामिल है।
Important Points about Command Line Arguments
Command-line arguments are passed to the
main()function when the program is executed.argcstores the total number of arguments, including the program name.argvis an array of character pointers.All command-line arguments are received as strings.
Numeric arguments must be converted into numeric data types before performing arithmetic operations.
If no additional argument is supplied,
argcis normally 1.argv[argc]is alwaysNULL.कमांड लाइन आर्गुमेंट प्रोग्राम चलाते समय
main()फंक्शन को प्रदान किए जाते हैं।argcमें प्रोग्राम के नाम सहित सभी आर्गुमेंट की कुल संख्या होती है।argvCharacter Pointers का एक Array है।सभी कमांड लाइन आर्गुमेंट String के रूप में प्राप्त होते हैं।
गणितीय ऑपरेशन करने से पहले Numeric Arguments को संबंधित Numeric Data Type में Convert करना आवश्यक है।
यदि कोई अतिरिक्त आर्गुमेंट नहीं दिया जाता है, तो सामान्यतः
argcका मान 1 होता है।argv[argc]का मान हमेशाNULLहोता है।
Comments
Post a Comment