C Programming - Command Line Arguments - Discussion
Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 1)
1.
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>
int main(int argc, char **argv)
{
printf("%c\n", **++argv);
return 0;
}
Discussion:
26 comments Page 1 of 3.
Cherry said:
1 decade ago
Seenu's answer is absolutely right.
i.e., Here
argv[0]=myprog
argv[1]=one
argv[2]=two
argv[3]=three
**++argv[] it is pre-increment, initially it shows "myprog"
Now,
After the pre-increment it shows "one"
But given printf("%c",**++argv[]);
c for character so that at a time it stores only one character
i.e "o"
If "%s" is there in printf then it stores whole string"one"
I hope you understand.
i.e., Here
argv[0]=myprog
argv[1]=one
argv[2]=two
argv[3]=three
**++argv[] it is pre-increment, initially it shows "myprog"
Now,
After the pre-increment it shows "one"
But given printf("%c",**++argv[]);
c for character so that at a time it stores only one character
i.e "o"
If "%s" is there in printf then it stores whole string"one"
I hope you understand.
(2)
Vineet said:
1 decade ago
argv have address of argv[0];
argv[0]="myprog";
argv[1]="one"
.
.
.
and so on...
*(*(++argv)));
++argv means address of argv[1] and now value at argv[1] is address of o of(one). then again value at that address is o.
argv[0]="myprog";
argv[1]="one"
.
.
.
and so on...
*(*(++argv)));
++argv means address of argv[1] and now value at argv[1] is address of o of(one). then again value at that address is o.
(1)
Deepak kumar said:
1 decade ago
argv--->is the address of 1st element of argv[]
*argv---->points to the first element of argv[]
*++argv--->points to the next element of argv[],and its also points to 1st element of *++argv ie j of jan
**++argv---->print j
*argv---->points to the first element of argv[]
*++argv--->points to the next element of argv[],and its also points to 1st element of *++argv ie j of jan
**++argv---->print j
Sravya said:
7 years ago
Good explanation @Cherry.
Sanjana said:
7 years ago
But argv[0] must be a program name right?
Nikhil n said:
8 years ago
Perfect answer @Seenu.
GORIPARTHI. Udaya lakshmi said:
8 years ago
How the answer comes o?
Priyanka said:
9 years ago
Consider a pointer *p for storing single dimension array. *++p points to next element in the array.
In the case of 2-dimensional array **p is used and **++p points to next word.
Finally coming to answer **argv[] is a 2-dimensional representation of command line arguments and,
**++argv[] points to next word. %c is used for printing character at that position. So, the answer is "o".
In the case of 2-dimensional array **p is used and **++p points to next word.
Finally coming to answer **argv[] is a 2-dimensional representation of command line arguments and,
**++argv[] points to next word. %c is used for printing character at that position. So, the answer is "o".
Swapnil said:
9 years ago
Thanks @Cherry.
Akhilesh said:
1 decade ago
*argv=argv[1]=argv+1.
So, ++argv[1]=*argv.
i.e, *(++(*argv)) = here value is incremented so it will Print next character provided %c should be there in printf.
So, ++argv[1]=*argv.
i.e, *(++(*argv)) = here value is incremented so it will Print next character provided %c should be there in printf.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers