C के Command Line Arguments
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि
argv[0]program नाम है और पहला real argumentargv[1]है। - पहले यह check किए बिना
argv[1]पढ़ना किargcकाफी बड़ा है। - एक argument को बिना convert किए एक number की तरह इस्तेमाल करना, क्योंकि हर
argventry एक string है।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions