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.
Sravya said:
4 years ago
Good explanation @Cherry.
Sanjana said:
5 years ago
But argv[0] must be a program name right?
Nikhil n said:
5 years ago
Perfect answer @Seenu.
GORIPARTHI. Udaya lakshmi said:
5 years ago
How the answer comes o?
Priyanka said:
6 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:
7 years ago
Thanks @Cherry.
Akhilesh said:
9 years 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.
Sunaina said:
9 years ago
Another question is :
What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog friday tuesday sunday
/* myprog.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
printf("%c", *++argv[1]);
return 0;
}
For this answer is "r".
Can anyone explain difference in these ?
As the confusion is that in this question after ++ we move to next string and in another question as I mentioned above we move to one character.... why?
What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog friday tuesday sunday
/* myprog.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
printf("%c", *++argv[1]);
return 0;
}
For this answer is "r".
Can anyone explain difference in these ?
As the confusion is that in this question after ++ we move to next string and in another question as I mentioned above we move to one character.... why?
Shalini said:
9 years ago
**argv is pointer to array of pointers.
*argv---->argv[0]---->myprog.
*argv+1-->argv[1]---->one.
.
.
.
argv having base address of "myprog".
++argv having base address of "one".
*++argv(dereference) prints "one".
**++argv(two times dereference) prints 'o'.
*argv---->argv[0]---->myprog.
*argv+1-->argv[1]---->one.
.
.
.
argv having base address of "myprog".
++argv having base address of "one".
*++argv(dereference) prints "one".
**++argv(two times dereference) prints 'o'.
Vineet said:
10 years 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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers