C Programming - Command Line Arguments - Discussion

Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 19)
19.
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample friday tuesday sunday
/* sample.c */
#include<stdio.h>

int main(int argc, char *argv[])
{
    printf("%c", *++argv[2] );
    return 0;
}
s
f
u
r
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
25 comments Page 3 of 3.

Harish said:   8 years ago
Precedence of operators "[ ]" is highest followed by "++" and then " * ".

So, Here 1st argv[2] will be evaluated which is the base address of " tuesday " that is address of 't'.

Now operator "++" will be evaluated. So,++argv[2] will point to the next address that is the address of 'u'.Now at last operator ' * ' will evaluate the value at that address that is ''u". So, the correct answer is ' u '.

Mohamed said:   8 years ago
I think argv[0] always contains the name of the program? isn't it?

Nikhil said:   8 years ago
I have doubt @Sushma how you take this value can you explain?

argv[0]=sample
argv[1]=friday
argv[2]=tuesday

Priyanka said:   7 years ago
In C;

argv[1]=sample
argv[2]=friday
argv[3]=tuesday

So, in Q *++argv[2] so argv[2] is f but after increment next char which is r.

Jhethalal said:   5 years ago
*++argv[2];

We can write this statement *++ *(argv+2);
Now it points to the *++(tuesday) ;

The ++ operator help to increase 1 character and now points to the u address.

*Help the char at this address that is u.
(1)


Post your comments here:

Your comments will be displayed after verification.