In C, **++argv moves to the first user argument and then dereferences twice. For the program below executed as: cmd> sample friday tuesday sunday, what character is printed? /* sample.c */ #include<stdio.h> int main(int argc, char argv[]) { printf("%c", ++argv); return 0; }

Difficulty: Easy

Correct Answer: f

Explanation:


Introduction / Context:
This tests pointer manipulation with argv and double dereference. argv is a pointer to pointers (char). After increment, it points at the first user argument; dereferencing once yields a char, and dereferencing twice yields the first character of that string.


Given Data / Assumptions:

  • Command line: sample friday tuesday sunday
  • Expression: **++argv


Concept / Approach:
++argv advances from &argv[0] to &argv[1]. * (single) yields argv[1] which is "friday". ** (double) indexes the first character of "friday". Hence the character printed is 'f'.


Step-by-Step Solution:

Before increment: argv[0]="sample". After ++argv: *argv == "friday". **argv == "friday"[0] == 'f'.


Verification / Alternative check:
Replacing with printf("%c", argv[1][0]); under the initial argv yields the same result 'f'.


Why Other Options Are Wrong:

  • s: That would be the first letter of "sample", not printed here.
  • sample / friday: The format is %c, which prints a single character, not a string.
  • Undefined behavior: With argc > 1 this is defined.


Common Pitfalls:
Mixing up pre-increment and array indexing order when working with pointer-to-pointer structures like argv.


Final Answer:
f

More Questions from Command Line Arguments

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion