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 1 of 3.

A.Rajesh said:   2 decades ago
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
o
rajesh@rajesh-laptop:~$

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.

Anand Shankar Jha said:   1 decade ago
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:   1 decade ago
Seenu answer is good and understood.

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

KHAGESH GUPTA said:   1 decade ago
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:   1 decade ago
Senu answer is good.

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

Ashok said:   1 decade ago
Seenu answer is correct.

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


Post your comments here:

Your comments will be displayed after verification.