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.

Neha said:   1 decade ago
Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux, and are passed in to the program from the operating system.

To use command line arguments in your program, you must first understand the full declaration of the main function, which previously has accepted no arguments. In fact, main can actually accept two arguments: one argument is number of command line arguments, and the other argument is a full list of all of the command line arguments.

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

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.

Shrikant said:   1 decade ago
argv[0]=sample
argv[1]=friday
argv[2]=tuesday

Here we have printf("%c", *++argv[2] );

argv[2] pointed to the "tuesday" and argv is contains the address of first location and ++ operator is adding two byte(because of int) in argv then it takes the address of next location(u) and * operator is use for printing the value of that location;

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.

Sarfaraz Ahamad said:   1 decade ago
argv[0]=sample
argv[1]=friday
argv[2]=tuesday

Here it points to "tuesday"
By default array will start from zero and by (++) controll shift at one possition so it already holds the char 'u' thats why answer will be 'u'.

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)

Paras Vanika said:   1 decade 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.

Suman Devi said:   1 decade ago
Shouldn't ++argv[2] be equivalent to a shift in the pointer by sizeof(char *)i.e. it should now be argv[3] and after dereferncing, it prints the first character of 4rth argument?

Sushma said:   1 decade ago
argv[0]=sample
argv[1]=friday
argv[2]=tuesday

Here we have printf("%c", *++argv[2] );

argv[2]=tuesday

So *++argv[2] points 2nd character in 'tuesday' that it 'u'.


Post your comments here:

Your comments will be displayed after verification.