C Programming - Command Line Arguments - Discussion


What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three

/* myprog.c */

int main(int argc, char **argv)
    printf("%s\n", *++argv);
    return 0;

[A]. myprog
[B]. one
[C]. two
[D]. three

Answer: Option B


No answer description available for this question.

Hariom said: (Oct 21, 2010)  
I think


++argv means argv[1]

thats why argv[1] = one

because of print string answer is one.

Jyotiranjan said: (Feb 21, 2011)  
Execution start from right argv could not increased. Compiler will take the value first then increment. As first value was one.

So answer is one.

Vidi said: (Jun 14, 2011)  
Can anyone elaborate on : char **argv[] used.

Ankur said: (Jul 1, 2011)  
Can someone explain this output in detail ?

Sholvi.. said: (Sep 25, 2011)  
I think here ...

argv[] means *argv and *argv[]=**argv
argv[0] or *argv contains address of string "myprog"

When we increament *argv it tends to next value stored as:
and as we can see in printf()=>"%s" is used so it will print the value of argv[1]
and we will get the ans:

Output: one

Rathika.B said: (Jan 16, 2012)  
All mention only about argv. Then what about argc? what it can do here? & in function definition must match the parameters passing in command prompt. But we directly declare the strings here as one two three then how it accept?

Saurabh said: (Mar 8, 2012)  
**argv is another notation for 2D array.

argv = address of 1st row

++argv = address of first row n end of 1st rows column
*++argv = value in first row

Thus output is: one

Rock said: (Dec 28, 2012)  
Here argc=3;

myprog is program name;

First pointer execute and after printing it will increase.

So answer is one.

Priya said: (Feb 12, 2013)  
Why is the argc mentioned here unnecessarily?

Rajashekar said: (Nov 10, 2013)  
Essentially argv always points to the arguments that have been entered on the command line and argc holds the no of arguments entered on the command line.

>>For every program ,program name itself is an argument that is passed to the OS to execute, so for any program always the first argument would always be the program name like:"c\users\tc\bin\hello.c"

>>So the arguments that later follow the program name are the rest of the arguments that are indexed by argv in order which they are entered.

argv[0] = myprog
argv[1] = one
argv[2] = two
argv[3] = three

>>So *++argv corresponds to argv[1] i.e===>one.

Klobin said: (Nov 14, 2013)  
argc is 4 here as ---- myprogs is also considered as no.1.

So the total becomes four ---- argument counter will get value:4.

Ganesh Sable Jtc said: (Jun 29, 2014)  
Rule of pointer *p++ is increment address.

Suppose int *p++ address is 2000 then increment 2004 because pointer size 4.

And *++p is increment actual value suppose 456 in array so display 5.

Abhinav Kumar said: (May 8, 2016)  
**++argv should be used instead of *++argv.

Vinayak Tapkir said: (Dec 3, 2017)  
Answer is wrong beacause first argument contains file executable name. Ao after incrementing it must print 1st provided argument on command line.

Anshu said: (Aug 28, 2018)  
Good explanation, Thanks @Sholvi.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.