← Back to C Course | Chapter 12: Advanced Topics | Lesson 5 of 20

C के Command Line Arguments

Command line arguments extra words हैं जो एक program शुरू करते समय इसके नाम के बाद type किए जाते हैं, एक helper को extra instructions देने जैसा। C इन्हें main को एक count और एक list के तौर पर देता है।
Syntax
c
int main(int argc, char *argv[]) {
  // argc: number of arguments
  // argv[0]: program name, argv[1]...: the arguments
  return 0;
}

Command Line Arguments क्या हैं?

Command line arguments एक user को terminal में program नाम के बाद extra words type करके source code edit या recompile किए बिना launch time पर एक program का behavior configure करने देते हैं।

इसी तरह compilers और file utilities जैसे tools filenames, flags, और options accept करते हैं।

उदाहरण: What are Command Line Arguments?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
	// Print formatted text to the screen
	printf("Program received %d arguments", argc);
	// Return 0 to signal that the program finished successfully
	return 0;
}

argc Parameter

argc program के launch होने पर arguments की total count रखता है, और यह हमेशा program के अपने नाम को पहली entry के तौर पर शामिल करता है, इसलिए दो extra words के साथ चला program argc 2 नहीं, 3 रखता है।

argv access करने से पहले argc check करना actually दी गई arguments से आगे पढ़ने से बचने के लिए essential है।

उदाहरण: The argc Parameter

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
	// Print formatted text to the screen
	printf("%d", argc);
	// Return 0 to signal that the program finished successfully
	return 0;
}

argv Parameter

argv actual argument text रखने वाले C-string pointers का एक array है, जहाँ argv[0] conventionally program का अपना invocation name है और argv[1] से आगे वे arguments हैं जो user ने actually type किए।

हर entry एक plain null-terminated string है, इसलिए आप इन्हें parse या compare करने के लिए standard string functions इस्तेमाल करते हैं।

उदाहरण: The argv Parameter

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
	// Print formatted text to the screen
	printf("%s", argv[0]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Argument Values पढ़ना

launch पर pass की गई arguments इस्तेमाल करने के लिए, position 1 से शुरू करके argv में index करें (index 0 पर program नाम skip करते हुए), अगर argument text की बजाय numeric input represent करता है तो हर string को atoi() या similar से एक number में convert करते हुए।

इन्हें उसी order में process करें जिस order में user ने दिया, क्योंकि argv उस order को exactly preserve करता है।

उदाहरण: Reading Argument Values

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
	char *fakeArgv[2] = {"prog", "42"};
	// Declare value and set it to atoi(fakeArgv[1])
	int value = atoi(fakeArgv[1]);
	// Print formatted text to the screen
	printf("%d", value);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Argument Counts Validate करना

क्योंकि argv सिर्फ इतना लंबा है जितना argc कहता है, argc-1 से ज़्यादा i के लिए argv[i] पढ़ना unallocated memory पढ़ता है और crash या unpredictably behave कर सकता है।

Actually pass की गई किसी भी index को छूने से पहले हमेशा expected argument count को argc से compare करें, और आगे बढ़ने की बजाय एक usage message print करें।

उदाहरण: Validating Argument Counts

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
	// Check whether argc < 2
	if (argc < 2) {
		// Print formatted text to the screen
		printf("Not enough arguments");
		// Send 1 back to the caller
		return 1;
	}
	// Print formatted text to the screen
	printf("%s", argv[1]);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि argv[0] program नाम है और पहला real argument argv[1] है।
  2. पहले यह check किए बिना argv[1] पढ़ना कि argc काफी बड़ा है।
  3. एक argument को बिना convert किए एक number की तरह इस्तेमाल करना, क्योंकि हर argv entry एक string है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.