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 2 of 2.
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
Ankur said:
1 decade ago
Can someone explain this output in detail ?
Vidi said:
1 decade ago
Can anyone elaborate on : char **argv[] used.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers