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
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;
}
Discussion:
15 comments Page 1 of 2.
Vinayak Tapkir said:
8 years ago
Answer is wrong beacause first argument contains file executable name. Ao after incrementing it must print 1st provided argument on command line.
(2)
Rajashekar said:
1 decade 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.
>>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.
(1)
Hariom said:
2 decades ago
I think
argv[0]=myprog
++argv means argv[1]
thats why argv[1] = one
because of print string answer is one.
argv[0]=myprog
++argv means argv[1]
thats why argv[1] = one
because of print string answer is one.
Jyotiranjan said:
1 decade ago
Execution start from right argv could not increased. Compiler will take the value first then increment. As first value was one.
So answer is one.
So answer is one.
Vidi said:
1 decade ago
Can anyone elaborate on : char **argv[] used.
Ankur said:
1 decade ago
Can someone explain this output in detail ?
Sholvi.. said:
1 decade ago
I think here ...
argv[] means *argv and *argv[]=**argv
argv[0] or *argv contains address of string "myprog"
*argv[0]="myprog"
When we increament *argv it tends to next value stored as:
*argv[1]="one"
and as we can see in printf()=>"%s" is used so it will print the value of argv[1]
and we will get the ans:
Output: one
argv[] means *argv and *argv[]=**argv
argv[0] or *argv contains address of string "myprog"
*argv[0]="myprog"
When we increament *argv it tends to next value stored as:
*argv[1]="one"
and as we can see in printf()=>"%s" is used so it will print the value of argv[1]
and we will get the ans:
Output: 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?
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
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
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.
myprog is program name;
First pointer execute and after printing it will increase.
So answer is one.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers