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.

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

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'.

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)

Akhilesh said:   1 decade ago
*argv=argv[1]=argv+1.

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

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

Seenu said:   2 decades ago
argv[0] = myprog

argv[1] = one

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

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

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

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

Sanjana said:   7 years ago
But argv[0] must be a program name right?

Navneet agarwal said:   1 decade ago
Seenu answer is good and understood.

Murthy said:   1 decade ago
Please give me brief description.


Post your comments here:

Your comments will be displayed after verification.