UGC-NET, GATE, NIELIT (O/A/B Level), KVS/NVS Computer Teacher, PGDCA, BCA, MCA, B-Tech व All India IT Exam विशेष नोट्स: इस अध्याय में हम Command-Line Arguments (argc, argv) and Variadic Functions in C (C में कमांड-लाइन आर्ग्युमेंट्स (argc, argv) और वेरिएडिक फंक्शन्स) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और राष्ट्रीय स्तर की परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Command-line arguments (argc, argv) allow C programs to receive configuration and input directly from the operating system shell at startup. Variadic functions (via <stdarg.h>) enable C functions like printf to accept a variable number of arguments using va_list, va_start, va_arg, and va_end.
अध्याय सार: कमांड-लाइन आर्ग्युमेंट्स (argc, argv) C प्रोग्राम को स्टार्टअप के समय सीधे ऑपरेटिंग सिस्टम शेल से इनपुट प्राप्त करने की सुविधा देते हैं। वेरिएडिक फंक्शन्स (<stdarg.h> के माध्यम से) printf जैसे C फंक्शन्स को va_list, va_start, va_arg और va_end का उपयोग करके परिवर्तनीय संख्या में आर्ग्युमेंट स्वीकार करने में सक्षम बनाते हैं।
Command-Line Arguments (argc, argv) and Variadic Functions in C: Detailed Notes & Architecture (C में कमांड-लाइन आर्ग्युमेंट्स (argc, argv) और वेरिएडिक फंक्शन्स)
1. Command-Line Arguments: argc and argv (कमांड-लाइन आर्ग्युमेंट्स: argc और argv)
[EN] The standard C entry point int main(int argc, char *argv[]) receives argc (argument count, always >= 1) and argv (argument vector, an array of C strings). By the C standard, argv[0] holds the program name (or empty string if unavailable) and argv[argc] is guaranteed to be a NULL pointer.
[HI] मानक C एंट्री पॉइंट int main(int argc, char *argv[]) में argc आर्ग्युमेंट्स की संख्या (हमेशा >= 1) और argv स्ट्रिंग्स का एरे होता है। C मानक के अनुसार argv[0] प्रोग्राम का नाम रखता है और argv[argc] हमेशा NULL पॉइंटर होता है।
2. Parsing Numeric Arguments Safely (संख्यात्मक आर्ग्युमेंट्स को सुरक्षित रूप से पार्स करना)
[EN] Because every element in argv is a char* string, numeric inputs must be converted using atoi() or preferably strtol()/strtod() for error detection. Unlike atoi(), strtol() detects invalid characters via endptr and overflows via errno == ERANGE.
[HI] चूंकि argv में हर तत्व एक char* स्ट्रिंग होता है, इसलिए संख्याओं को बदलने के लिए atoi() या बेहतर त्रुटि जांच के लिए strtol()/strtod() का उपयोग किया जाता है। atoi() के विपरीत, strtol() अवैध अक्षरों और ओवरफ्लो का पता लगा सकता है।
3. Variadic Functions and <stdarg.h> Macros (वेरिएडिक फंक्शन्स और <stdarg.h> मैक्रोज़)
[EN] A variadic function declares at least one named parameter followed by an ellipsis (...), such as int sum(int count, ...). Inside, you declare va_list ap;, initialize with va_start(ap, last_named_arg), read values with va_arg(ap, type), and clean up with va_end(ap).
[HI] वेरिएडिक फंक्शन में कम से कम एक नामित पैरामीटर और उसके बाद एलिप्सिस (...) होता है, जैसे int sum(int count, ...)। इसके अंदर va_list ap; घोषित करके va_start, va_arg और अंत में va_end का उपयोग किया जाता है।
4. Default Argument Promotions and Safety Pitfalls (डिफ़ॉल्ट आर्ग्युमेंट प्रमोशन और सुरक्षा सावधानियां)
[EN] Arguments passed through (...) undergo default argument promotions: char/short promote to int, and float promotes to double; reading float instead of double in va_arg causes Undefined Behavior. Because C does not pass argument count or types automatically, callers must supply a count or format string.
[HI] (...) के माध्यम से पास किए गए आर्ग्युमेंट्स में डिफ़ॉल्ट प्रमोशन होता है: char/short int में और float double में बदल जाता है; va_arg में float पढ़ने पर Undefined Behavior होता है। चूंकि C में टाइप या संख्या स्वतः नहीं मिलती, इसलिए काउंट या फॉर्मेट स्ट्रिंग देना जरूरी है।
Syntax, Formula & Technical Example (Command-Line Arguments (argc, argv) and Variadic Functions in C)
#include <stdio.h>
#include <stdarg.h>
int sum_all(int count, ...) {
va_list ap; int total = 0;
va_start(ap, count);
for (int i = 0; i < count; i++) total += va_arg(ap, int);
va_end(ap);
return total;
}
int main(int argc, char *argv[]) { printf("%d %s\n", argc, argv[0]); }
Command-Line Arguments (argc, argv) and Variadic Functions in C Quick Reference & Comparison Table
| Construct / Macro | Header / Signature | Behavior & Exam Note |
|---|---|---|
| argc | int argc in main() | Total arguments >= 1 (includes program name) |
| argv[argc] | char *argv[] in main() | Guaranteed by C standard to be NULL |
| va_start(ap, last) | <stdarg.h> macro (init) | Initializes va_list after last fixed parameter |
| va_arg(ap, type) | <stdarg.h> macro (read) | Fetches next argument; float promotes to double |
Command-Line Arguments (argc, argv) and Variadic Functions in C Top 20 Bilingual MCQs with Answers
Q1. If you run `./app input.txt 42` in a terminal, what is the value of `argc` inside main()?
(यदि आप टर्मिनल में `./app input.txt 42` चलाते हैं, तो main() के अंदर `argc` का मान क्या होगा?)
(A) 2
(B) 4
(C) 1
(D) 3
Correct Answer: (D)
[EN] argc counts the program name (./app) plus the two arguments (input.txt, 42), totaling 3.
[HI] argc में प्रोग्राम का नाम (./app) और दो आर्ग्युमेंट (input.txt, 42) मिलाकर कुल 3 गिने जाते हैं।
Q2. According to the C standard, what is `argv[argc]` guaranteed to be?
(C मानक के अनुसार, `argv[argc]` का मान निश्चित रूप से क्या होता है?)
(A) Garbage value
(B) The last user argument
(C) NULL pointer
(D) Empty string ""
Correct Answer: (C)
[EN] The C standard mandates that argv[argc] is a null pointer, allowing loops like while (*argv) { ... }.
[HI] C मानक के अनुसार argv[argc] हमेशा एक NULL पॉइंटर होता है।
Q3. Which standard header file must be included to write variadic functions in C?
(C में वेरिएडिक फंक्शन लिखने के लिए किस मानक हेडर फाइल को शामिल करना आवश्यक है?)
(A) <stdarg.h>
(B) <stdint.h>
(C) <varargs.h>
(D) <stdlib.h>
Correct Answer: (A)
[EN] ANSI C / ISO C uses <stdarg.h> (the older <varargs.h> is obsolete).
[HI] मानक C में वेरिएडिक फंक्शन के लिए <stdarg.h> का उपयोग किया जाता है।
Q4. What does `argv[0]` normally hold when a C program is executed from a shell?
(शेल से C प्रोग्राम चलाने पर `argv[0]` में सामान्यतः क्या होता है?)
(A) The total number of arguments
(B) The name or path used to invoke the program
(C) The process ID (PID)
(D) The first command-line flag
Correct Answer: (B)
[EN] argv[0] stores the invocation name/path of the executable itself.
[HI] argv[0] में प्रोग्राम को चलाने के लिए उपयोग किया गया नाम या पाथ होता है।
Q5. Which macro initializes a `va_list` variable before reading variadic arguments?
(वेरिएडिक आर्ग्युमेंट पढ़ने से पहले `va_list` वेरिएबल को इनिशियलाइज़ करने के लिए किस मैक्रो का उपयोग होता है?)
(A) va_init
(B) va_start
(C) va_copy
(D) va_begin
Correct Answer: (B)
[EN] va_start(ap, last_fixed_param) sets ap to point to the first unnamed argument.
[HI] va_start(ap, last_fixed_param) वेरिएडिक सूची को इनिशियलाइज़ करता है।
Q6. Due to default argument promotion in C variadic functions, what type must you pass to `va_arg` if the caller passed a `float`?
(C वेरिएडिक फंक्शन्स में डिफ़ॉल्ट आर्ग्युमेंट प्रमोशन के कारण, यदि कॉलर ने `float` पास किया है तो `va_arg` में कौन सा टाइप देना चाहिए?)
(A) long double
(B) int
(C) double
(D) float
Correct Answer: (C)
[EN] In (...), float automatically promotes to double, and char/short promote to int.
[HI] (...) में float स्वतः double में प्रमोट हो जाता है, इसलिए va_arg(ap, double) का उपयोग करना चाहिए।
Q7. What happens if a variadic function passes `char` and you read it with `va_arg(ap, char)`?
(यदि वेरिएडिक फंक्शन में `char` पास किया जाए और आप उसे `va_arg(ap, char)` से पढ़ें तो क्या होता है?)
(A) It returns 0 safely
(B) Undefined Behavior (because char is promoted to int)
(C) It compiles and works portably on all CPUs
(D) It converts char to float
Correct Answer: (B)
[EN] Default argument promotion converts char/short to int; passing a narrow type to va_arg triggers Undefined Behavior.
[HI] char स्वतः int में प्रमोट हो जाता है, इसलिए va_arg(ap, char) लिखने से Undefined Behavior होता है।
Q8. Which macro must be called before returning from a variadic function to clean up the `va_list`?
(वेरिएडिक फंक्शन से लौटने से पहले `va_list` को क्लीन-अप करने के लिए किस मैक्रो को कॉल करना अनिवार्य है?)
(A) va_stop
(B) va_end
(C) va_free
(D) va_close
Correct Answer: (B)
[EN] Every va_start (or va_copy) must be paired with va_end(ap) in the same function.
[HI] प्रत्येक va_start के बाद उसी फंक्शन में va_end(ap) को कॉल करना आवश्यक है।
Q9. Which C library function is safer than `atoi()` for converting `argv[1]` into an integer with error checking?
(त्रुटि जांच के साथ `argv[1]` को पूर्णांक में बदलने के लिए `atoi()` से अधिक सुरक्षित C लाइब्रेरी फंक्शन कौन सा है?)
(A) sprintf()
(B) itoa()
(C) gets()
(D) strtol()
Correct Answer: (D)
[EN] strtol() reports invalid characters via endptr and overflow via errno, whereas atoi() has undefined behavior on overflow.
[HI] strtol() अवैध अक्षरों और ओवरफ्लो की जांच कर सकता है, जबकि atoi() ऐसा नहीं करता।
Q10. Can a C variadic function have zero fixed (named) parameters, such as `void foo(...)` in C17/C99?
(क्या C99/C17 में बिना किसी नामित पैरामीटर के `void foo(...)` जैसा वेरिएडिक फंक्शन मान्य है?)
(A) Yes, in ANSI C89
(B) Only inside main()
(C) Yes, because va_start takes 0 arguments
(D) No, C99/C17 requires at least one named parameter before `...` (though C23 allows it)
Correct Answer: (D)
[EN] In C89/C99/C17, at least one named parameter is required so va_start knows where `...` begins (C23 relaxed this).
[HI] C99/C17 में `...` से पहले कम से कम एक नामित पैरामीटर होना अनिवार्य है ताकि va_start को शुरुआती पता मिल सके।
Q11. Which macro introduced in C99 duplicates a `va_list` so it can be traversed multiple times?
(C99 में पेश किया गया कौन सा मैक्रो `va_list` की कॉपी बनाता है ताकि उसे एक से अधिक बार पढ़ा जा सके?)
(A) va_copy
(B) va_clone
(C) va_reset
(D) va_dup
Correct Answer: (A)
[EN] va_copy(dest, src) duplicates the current state of src into dest (which must also call va_end).
[HI] va_copy(dest, src) va_list की स्थिति को कॉपी करता है।
Q12. What is the type of `argv` in `int main(int argc, char *argv[])`?
(`int main(int argc, char *argv[])` में `argv` का वास्तविक डेटा टाइप क्या होता है?)
(A) int *
(B) char * (single string)
(C) void *
(D) char ** (pointer to pointer to char)
Correct Answer: (D)
[EN] As a function parameter, char *argv[] decays to char **.
[HI] फंक्शन पैरामीटर के रूप में char *argv[] का अर्थ char ** (कैरेक्टर पॉइंटर का पॉइंटर) होता है।
Q13. Why is `vprintf(const char *fmt, va_list ap)` provided alongside `printf` in <stdio.h>?
(<stdio.h> में `printf` के साथ `vprintf(const char *fmt, va_list ap)` क्यों दिया जाता है?)
(A) So custom variadic wrapper functions (like custom loggers) can forward their va_list
(B) To disable buffering
(C) To run printf in a separate thread
(D) Because printf cannot print integers
Correct Answer: (A)
[EN] Variadic functions cannot forward `...` directly to another variadic function; they pass a `va_list` to `v*` variants.
[HI] एक वेरिएडिक फंक्शन सीधे `...` को दूसरे फंक्शन में नहीं भेज सकता, इसलिए लॉगिंग रैपर बनाने के लिए va_list को vprintf में पास किया जाता है।
Q14. In `int main(int argc, char **argv, char **envp)`, what does the non-standard third parameter `envp` contain on POSIX/Windows?
(`int main(int argc, char **argv, char **envp)` में तीसरा पैरामीटर `envp` क्या रखता है?)
(A) CPU register values
(B) Environment variables (key=value strings) terminated by NULL
(C) Exit codes of previous programs
(D) Stack trace addresses
Correct Answer: (B)
[EN] envp is an array of environment variable strings (like PATH=...), though getenv() is the portable standard way.
[HI] envp सिस्टम के एनवायरनमेंट वेरिएबल्स (key=value स्ट्रिंग्स) की सूची रखता है।
Q15. What happens if you pass fewer arguments to a variadic function than the format string or count parameter expects?
(यदि आप वेरिएडिक फंक्शन में काउंट या फॉर्मेट स्ट्रिंग की अपेक्षा से कम आर्ग्युमेंट पास करते हैं तो क्या होता है?)
(A) Undefined Behavior (va_arg reads garbage memory/registers)
(B) C automatically pads remaining arguments with 0
(C) va_arg returns NULL
(D) Compiler always rejects it with a syntax error
Correct Answer: (A)
[EN] va_arg has no runtime bounds check and reads out-of-bounds stack/register data, causing Undefined Behavior.
[HI] va_arg में रनटाइम बाउंड चेक नहीं होता, इसलिए कम आर्ग्युमेंट देने पर यह कचरा मेमोरी पढ़ता है (Undefined Behavior)।
Q16. Which of the following statements about C command-line arguments and variadic functions is NOT true?
(C कमांड-लाइन आर्ग्युमेंट्स और वेरिएडिक फंक्शन्स के बारे में निम्नलिखित में से कौन सा कथन सत्य नहीं है?)
(A) argv[0] typically holds the program name
(B) The C compiler automatically passes the count of variadic arguments inside `va_list`
(C) va_end(ap) must be called before the variadic function returns
(D) argv[argc] is guaranteed to be NULL
Correct Answer: (B)
[EN] C variadic functions have no hidden argument-count metadata; the programmer must pass a count, sentinel, or format string.
[HI] C कंपाइलर `va_list` में आर्ग्युमेंट्स की संख्या स्वतः नहीं भेजता; प्रोग्रामर को काउंट, सेंटिनल या फॉर्मेट स्ट्रिंग देनी पड़ती है।
Q17. Within Command-Line Arguments (argc, argv) and Variadic Functions in C, which construct / macro directly maps to: "int argc in main()"?
(Command-Line Arguments (argc, argv) and Variadic Functions in C में कौन सा Construct / Macro सीधे "int argc in main()" से संबंधित है?)
(A) va_arg(ap, type)
(B) va_start(ap, last)
(C) argc
(D) argv[argc]
Correct Answer: (C)
[EN] In Command-Line Arguments (argc, argv) and Variadic Functions in C, 'argc' handles 'int argc in main()' (Total arguments >= 1 (includes program name)).
[HI] Command-Line Arguments (argc, argv) and Variadic Functions in C में 'argc' का कार्य/विवरण 'int argc in main()' (Total arguments >= 1 (includes program name)) है।
Q18. Which option accurately describes 'argv[argc]' in Command-Line Arguments (argc, argv) and Variadic Functions in C?
(Command-Line Arguments (argc, argv) and Variadic Functions in C के संदर्भ में 'argv[argc]' की सही विशेषता कौन सी है?)
(A) char *argv[] in main()
(B) <stdarg.h> macro (read)
(C) <stdarg.h> macro (init)
(D) int argc in main()
Correct Answer: (A)
[EN] 'argv[argc]' provides 'char *argv[] in main()' — key point: Guaranteed by C standard to be NULL.
[HI] 'argv[argc]' का मुख्य विवरण 'char *argv[] in main()' है (Guaranteed by C standard to be NULL)।
Q19. In Command-Line Arguments (argc, argv) and Variadic Functions in C, identify the correct parameter for the specification: "<stdarg.h> macro (init)".
(Command-Line Arguments (argc, argv) and Variadic Functions in C में "<stdarg.h> macro (init)" विवरण किस विकल्प के लिए सही है?)
(A) argv[argc]
(B) va_start(ap, last)
(C) va_arg(ap, type)
(D) argc
Correct Answer: (B)
[EN] 'va_start(ap, last)' corresponds to '<stdarg.h> macro (init)' (Initializes va_list after last fixed parameter).
[HI] 'va_start(ap, last)' का संबंध '<stdarg.h> macro (init)' (Initializes va_list after last fixed parameter) से है।
Q20. Considering Command-Line Arguments (argc, argv) and Variadic Functions in C, what key behavior defines 'va_arg(ap, type)'?
(Command-Line Arguments (argc, argv) and Variadic Functions in C में 'va_arg(ap, type)' की मुख्य भूमिका या मान क्या है?)
(A) <stdarg.h> macro (init)
(B) int argc in main()
(C) <stdarg.h> macro (read)
(D) char *argv[] in main()
Correct Answer: (C)
[EN] 'va_arg(ap, type)' represents '<stdarg.h> macro (read)' (Fetches next argument; float promotes to double).
[HI] 'va_arg(ap, type)' का अर्थ/मान '<stdarg.h> macro (read)' है (Fetches next argument; float promotes to double)।
Command-Line Arguments (argc, argv) and Variadic Functions in C — Exam FAQs
Q1: Why is `int main(int argc, char *argv[])` preferred over `void main()`?
`void main()` की तुलना में `int main(int argc, char *argv[])` को प्राथमिकता क्यों दी जाती है?
[EN] The ISO C standard requires main() to return int (0 for success) to the OS; void main() is non-standard in hosted environments.
[HI] ISO C मानक के अनुसार main() को OS को int (सफलता के लिए 0) लौटाना चाहिए; void main() मानक नहीं है।
Q2: How does `printf` know how many arguments and what types were passed to it?
`printf` को कैसे पता चलता है कि उसे कितने और किस प्रकार के आर्ग्युमेंट पास किए गए हैं?
[EN] It parses format specifiers (%d, %s, %f) in its first fixed string argument and calls va_arg with the matching promoted type for each.
[HI] यह अपने पहले फॉर्मेट स्ट्रिंग आर्ग्युमेंट (%d, %s, %f) को पढ़कर पता लगाता है कि आगे va_arg से किस टाइप का मान निकालना है।
Q3: What if a command-line argument itself contains spaces (e.g., a file path)?
यदि किसी कमांड-लाइन आर्ग्युमेंट में स्पेस हो (जैसे फाइल पाथ) तो क्या होगा?
[EN] Wrap it in quotes in the shell (e.g., `./app "my file.txt"`), so the shell passes it as a single argv[1] string.
[HI] शेल में उसे कोट्स में लिखें (जैसे `./app "my file.txt"`), ताकि शेल उसे एक ही argv[1] स्ट्रिंग के रूप में पास करे।
Post a Comment
Post a Comment