You are here: Home / Hindi Web /Topics / Chapter - 10 Command line arguments in C programming in hindi

Chapter - 10 Command line arguments in C programming in hindi

Filed under: C Programming Tutorials on 2022-07-19 09:48:23
  • Introduction to C command line arguments in Hindi
  • Syntax of C command line arguments in Hindi
  • Example of C command line arguments in Hindi

Introduction to C Command Line Arguments

ऐसे arguments जो किसी C program में main() function को pass किये जाते है command line arguments कहलाते है। इन arguments को program को run करते समय pass किया जाता है।

अभी तक आपने C programs में कई बार main() function define किया होगा। किसी भी program में main() function सबसे महत्वपूर्ण function होता है। लेकिन क्या आप जानते है की किसी normal function की ही तरह main() function को भी arguments pass किये जा सकते है? 

किसी program में main() function को arguments pass करना कई कारणों से लाभदायक हो सकता है। उदाहरण के लिए आप कोई ऐसी application create करना चाहते है जो commands को execute करे और उनके अनुसार operations perform करें तो इसके लिए main() function को argument pass करके उसमे अपना मनचाहा operation perform किया जा सकता है। 

इसके अलावा इससे एक benefit यह और है की आप program को run करने से पूर्व ही आप बता देते है की आप क्या करना चाहते है। जब आप command line arguments का प्रयोग करते है तो आपको input window के लिए wait करने की आवश्यकता नहीं होती है आप program के बाहर से ही input provide कर देते है और उसी के अनुसार program execute हो जाता है। 

Command line arguments द्वारा किसी program को बाहर से ही control किया जा सकता है और commands (arguments) pass करके application को चलाया जा सकता है। 

उदाहरण के लिए आप कोई ऐसा program create कर रहे है जो किसी process को start और stop करता है तो इसके लिए आप command line arguments use कर सकते है और program को run करते समय ही start या stop commands द्वारा process और control कर सकते है।

Syntax of C Command Line Arguments

C programs में command line arguments define करने के लिए आप निचे दिया जा रहा syntax use कर सकते है

int main(int argc, char *argv[])
{
    //rest of code
}

ऊपर दिए गए code में main() function को command line arguments के साथ define किया गया है। जब आप अपने program में command line arguments accept करना चाहते है तो इसके आप लिए argc और argv parameters define करते है जैसा की ऊपर दिए गए code में किया गया है।

इसमें argc एक integer number होता है जो यह बताता है की कितने arguments main() function को pass किये जा रहे है। इसके अलावा argv एक pointer array होता है जो pass किये गए arguments को point करता है।

एक बात आपको हमेशा ध्यान रखनी चाहिए की argv[] array की zero index (argv[0]) हमेशा program का नाम बताती है और first index (argv[1]) पहले command line argument को दर्शाती है।

Command line argument pass करने के लिए आप program के नाम के बाद arguments को comma से separate करके लिखते है। इसका syntax निचे दिया जा रहा है।

program-name arg1,arg2, arg3…argN;

यदि कोई argument नहीं pass किया जाता है तो argc की value 1 रहती है। यदि आप एक argument pass करते है तो argc की value 2 हो जाती है। इसी प्रकार आप जितने arguments pass करते है argc की value उनसे एक अधिक होती है।

Example of C Command Line Arguments

C language में command line arguments के use को निचे उदाहरण द्वारा समझाया जा रहा है।

#include<stdio.h>
/* Defining command line arguments in main() function */
int main(int argc, char *argv[])
{
    printf(“Hello %s”,argv[1]);
}

ऊपर दिए गए program को execute करते समय program के नाम के बाद user argument के रूप में अपना नाम pass करता है और यह program user को welcome message show करता है।

cmdlineDemo Reader

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।

Hello Reader

Learn C programming MCQ Questions.

About Author:
M
Madhu     View Profile
Trust everyone but don't trust blindly