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("%c\n", **++argv);
    return 0;

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

Answer: Option C


No answer description available for this question.

A.Rajesh said: (Sep 13, 2010)  
I think here directory #include<stdlib.h> shouldn't mentioned.

But I tried in GCC compiler(linux) i got the out put like this

rajesh@rajesh-laptop:~$ vim prog.c
rajesh@rajesh-laptop:~$ cc prog.c
rajesh@rajesh-laptop:~$ ./a.out one two three

Seenu said: (Sep 17, 2010)  
argv[0] = myprog

argv[1] = one

So **++argv=> argv[1].

And %c is output so first char of one ==> o.

Anand Shankar Jha said: (Jan 6, 2011)  
I think seenu's logic is good enough to answer the question.
Good seenu.
since ++ is prefix with argv hence pre increament happens in argument and the the array is char type, so, it stores one char at a time.
So, Answer must be C i.e. "o".

Navneet Agarwal said: (Feb 9, 2011)  
Seenu answer is good and understood.

Murthy said: (Mar 18, 2011)  
Please give me brief description.

Khagesh Gupta said: (Mar 28, 2011)  
Real thing is here

*argv=pointer to "myprog"(=base address) and equivalent to argv[0]

then preincrements

*++argv=pointer to "one"(more specifically pointer to "o") equivalent to argv[1]

**++argv=value at this address

thus ans is o

Satya said: (Jun 7, 2011)  
Senu answer is good.

Gowri Shankar said: (Aug 2, 2011)  
Seenu your answer is acceptable, but Khagesh Gupta is correct.

Ashok said: (Oct 7, 2011)  
Seenu answer is correct.

Shambhu said: (Nov 10, 2011)  
I think seenu is correct because argv pointing to the first element that is myprog, when it is incremented (++ has higher priority than *), now it pointed to "one",but we have agian one * that will point to 'o'.

Vivek said: (Dec 4, 2011)  
Seenu your answer is corretct n understand. Good explain.

Rupinderjit said: (Dec 6, 2011)  
Here one thing to be taken into consideration is PRECEDENCE.Since ++ has high precedence over *(indrection),so pre-incrementation takes place first.and *argv==**argv,since both pointing to same address.
Khagesh Gupta alucidation in not so correct.

Moreover if we use array indexing here,then ++argv[i] means increment the value at argv[i] and display it.Andargv[i]++ means,value at argv[i] then increment the address using post increment protocol.

Shahul Hameed P (Sinuxcreation@Gmail.Com) said: (Jan 5, 2012)  
**++argv will point to argv[1]

If again we do that (ie, **++argv) it will point to argv[2]

Here %c is the control string,So output will be the values present at their initial location[ie,if pointing to argv[1] it will show the character present at 0th location of argv[1]).

Deepak Kumar said: (Mar 4, 2012)  
argv--->is the address of 1st element of argv[]
*argv---->points to the first element of argv[]
*++argv--->points to the next element of argv[],and its also points to 1st element of *++argv ie j of jan
**++argv---->print j

Cherry said: (Jul 1, 2012)  
Seenu's answer is absolutely right.
i.e., Here





**++argv[] it is pre-increment, initially it shows "myprog"

After the pre-increment it shows "one"
But given printf("%c",**++argv[]);

c for character so that at a time it stores only one character
i.e "o"

If "%s" is there in printf then it stores whole string"one"

I hope you understand.

Sadanand said: (Jul 11, 2012)  
**++argv means argv[0] but this will give first chareacter of argv[0].. In this case the value is 0.
If you want second character of that same string you can write like this *(*(argv+0)+2)

Vineet said: (Mar 18, 2013)  
argv have address of argv[0];
and so on...

++argv means address of argv[1] and now value at argv[1] is address of o of(one). then again value at that address is o.

Shalini said: (Nov 5, 2013)  
**argv is pointer to array of pointers.
argv having base address of "myprog".
++argv having base address of "one".
*++argv(dereference) prints "one".
**++argv(two times dereference) prints 'o'.

Sunaina said: (Jan 29, 2014)  
Another question is :
What will be the output of the program (myprog.c) given below if it is executed from the command line?

cmd> myprog friday tuesday sunday

/* myprog.c */

int main(int argc, char *argv[])
printf("%c", *++argv[1]);
return 0;

For this answer is "r".

Can anyone explain difference in these ?

As the confusion is that in this question after ++ we move to next string and in another question as I mentioned above we move to one character.... why?

Akhilesh said: (Apr 20, 2014)  

So, ++argv[1]=*argv.

i.e, *(++(*argv)) = here value is incremented so it will Print next character provided %c should be there in printf.

Swapnil said: (Jun 13, 2016)  
Thanks @Cherry.

Priyanka said: (Sep 2, 2016)  
Consider a pointer *p for storing single dimension array. *++p points to next element in the array.

In the case of 2-dimensional array **p is used and **++p points to next word.

Finally coming to answer **argv[] is a 2-dimensional representation of command line arguments and,

**++argv[] points to next word. %c is used for printing character at that position. So, the answer is "o".

Goriparthi. Udaya Lakshmi said: (Jan 7, 2018)  
How the answer comes o?

Nikhil N said: (Jan 25, 2018)  
Perfect answer @Seenu.

Sanjana said: (Jul 16, 2018)  
But argv[0] must be a program name right?

Sravya said: (Aug 27, 2018)  
Good explanation @Cherry.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.