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.
Sunaina said:
1 decade 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?
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)
Rupinderjit said:
1 decade ago
Here one thing to be taken into consideration is PRECEDENCE.Since ++ has high precedence over *(indrection),so pre-incrementation takes place first.and *argv==**argv,since both pointing to same address.
Khagesh Gupta alucidation in not so correct.
Moreover if we use array indexing here,then ++argv[i] means increment the value at argv[i] and display it.Andargv[i]++ means,value at argv[i] then increment the address using post increment protocol.
Khagesh Gupta alucidation in not so correct.
Moreover if we use array indexing here,then ++argv[i] means increment the value at argv[i] and display it.Andargv[i]++ means,value at argv[i] then increment the address using post increment protocol.
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".
Shalini said:
1 decade 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'.
Shahul Hameed P (sinuxcreation@gmail.com) said:
1 decade ago
**++argv will point to argv[1]
If again we do that (ie, **++argv) it will point to argv[2]
Here %c is the control string,So output will be the values present at their initial location[ie,if pointing to argv[1] it will show the character present at 0th location of argv[1]).
If again we do that (ie, **++argv) it will point to argv[2]
Here %c is the control string,So output will be the values present at their initial location[ie,if pointing to argv[1] it will show the character present at 0th location of argv[1]).
A.Rajesh said:
2 decades ago
I think here directory #include<stdlib.h> shouldn't mentioned.
But I tried in GCC compiler(linux) i got the out put like this
rajesh@rajesh-laptop:~$ vim prog.c
rajesh@rajesh-laptop:~$ cc prog.c
rajesh@rajesh-laptop:~$ ./a.out one two three
o
rajesh@rajesh-laptop:~$
But I tried in GCC compiler(linux) i got the out put like this
rajesh@rajesh-laptop:~$ vim prog.c
rajesh@rajesh-laptop:~$ cc prog.c
rajesh@rajesh-laptop:~$ ./a.out one two three
o
rajesh@rajesh-laptop:~$
KHAGESH GUPTA said:
1 decade ago
Real thing is here
*argv=pointer to "myprog"(=base address) and equivalent to argv[0]
then preincrements
*++argv=pointer to "one"(more specifically pointer to "o") equivalent to argv[1]
**++argv=value at this address
thus ans is o
*argv=pointer to "myprog"(=base address) and equivalent to argv[0]
then preincrements
*++argv=pointer to "one"(more specifically pointer to "o") equivalent to argv[1]
**++argv=value at this address
thus ans is o
Anand Shankar Jha said:
1 decade ago
I think seenu's logic is good enough to answer the question.
Good seenu.
since ++ is prefix with argv hence pre increament happens in argument and the the array is char type, so, it stores one char at a time.
So, Answer must be C i.e. "o".
Good seenu.
since ++ is prefix with argv hence pre increament happens in argument and the the array is char type, so, it stores one char at a time.
So, Answer must be C i.e. "o".
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers