C Programming - Command Line Arguments - Discussion

Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 2)
2.
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>
#include<stdlib.h>

int main(int argc, char **argv)
{
    printf("%s\n", *++argv);
    return 0;
}
myprog
one
two
three
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
15 comments Page 1 of 2.

Anshu said:   4 years ago
Good explanation, Thanks @Sholvi.

Vinayak Tapkir said:   5 years ago
Answer is wrong beacause first argument contains file executable name. Ao after incrementing it must print 1st provided argument on command line.

Abhinav Kumar said:   7 years ago
**++argv should be used instead of *++argv.

Ganesh sable jtc said:   9 years ago
Rule of pointer *p++ is increment address.

Suppose int *p++ address is 2000 then increment 2004 because pointer size 4.

And *++p is increment actual value suppose 456 in array so display 5.

Klobin said:   9 years ago
argc is 4 here as ---- myprogs is also considered as no.1.

So the total becomes four ---- argument counter will get value:4.

Rajashekar said:   9 years ago
Essentially argv always points to the arguments that have been entered on the command line and argc holds the no of arguments entered on the command line.

>>For every program ,program name itself is an argument that is passed to the OS to execute, so for any program always the first argument would always be the program name like:"c\users\tc\bin\hello.c"

>>So the arguments that later follow the program name are the rest of the arguments that are indexed by argv in order which they are entered.

argv[0] = myprog
argv[1] = one
argv[2] = two
argv[3] = three

>>So *++argv corresponds to argv[1] i.e===>one.

Priya said:   10 years ago
Why is the argc mentioned here unnecessarily?

Rock said:   1 decade ago
Here argc=3;

myprog is program name;

First pointer execute and after printing it will increase.

So answer is one.

Saurabh said:   1 decade ago
**argv is another notation for 2D array.

argv = address of 1st row

++argv = address of first row n end of 1st rows column
*++argv = value in first row

Thus output is: one

Rathika.b said:   1 decade ago
All mention only about argv. Then what about argc? what it can do here? & in function definition must match the parameters passing in command prompt. But we directly declare the strings here as one two three then how it accept?


Post your comments here:

Your comments will be displayed after verification.