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

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)

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.

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

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

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

Amol said:   8 years ago
Thanks @Sushma.
(1)

Elayaraja said:   9 years ago
The Precedence of dereference operator(*) and increment operator(++) is same.So we see associativity of these operators is right to left.

*++argv[2];
argv[2] is Tuesday here argv is point to base address of "tuesday"

When we do increment it'll point to next location content 'u' since argv is a character pointer
now we do dereference of this operator. Then we'll get u.

Manjunath(mj) said:   10 years ago
This will print s.

How it will print u friends please any one explain me?

Naga Vikas said:   1 decade ago
But in Dev C++, it is showing "r".

I guess it differs from compiler to compiler.

argv[0]--shows the path.

1-sample.
2-friday.
3-tuesday.
4-sunday.

argv[2]----friday.
++agrv[2] is pointing at "r".
*++argv[2]...prints the value r.

Balaji said:   1 decade ago
@Sumit: It will print null('\0') character. i.e. blank space will be printed.


Post your comments here:

Your comments will be displayed after verification.