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 2 of 3.
Vivek said:
1 decade ago
Seenu your answer is corretct n understand. Good explain.
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.
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]).
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
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)
Sadanand said:
1 decade ago
**++argv means argv[0] but this will give first chareacter of argv[0].. In this case the value is 0.
If you want second character of that same string you can write like this *(*(argv+0)+2)
If you want second character of that same string you can write like this *(*(argv+0)+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)
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'.
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?
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