C Programming - Command Line Arguments - Discussion

Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 1)
1.
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 */
#include<stdio.h>

int main(int argc, char **argv)
{
    printf("%c\n", **++argv);
    return 0;
}
myprog one two three
myprog one
o
two
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
26 comments Page 2 of 3.

Sadanand said:   1 decade ago
**++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)

Cherry said:   1 decade ago
Seenu's answer is absolutely right.
i.e., Here

argv[0]=myprog

argv[1]=one

argv[2]=two

argv[3]=three

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

Now,
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.
(2)

Deepak kumar said:   1 decade ago
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

Shahul Hameed P (sinuxcreation@gmail.com) said:   1 decade ago
**++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]).

Rupinderjit said:   1 decade ago
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.

Vivek said:   1 decade ago
Seenu your answer is corretct n understand. Good explain.

Shambhu said:   1 decade ago
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'.

Ashok said:   1 decade ago
Seenu answer is correct.

Gowri shankar said:   1 decade ago
Seenu your answer is acceptable, but Khagesh Gupta is correct.

Satya said:   1 decade ago
Senu answer is good.


Post your comments here:

Your comments will be displayed after verification.