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.
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?
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'.
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)
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:~$
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]).
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.
Vivek said:
1 decade ago
Seenu your answer is corretct n understand. Good explain.
Shambhu said:
1 decade ago
I think seenu is correct because argv pointing to the first element that is myprog, when it is incremented (++ has higher priority than *), now it pointed to "one",but we have agian one * that will point to 'o'.
Ashok said:
1 decade ago
Seenu answer is correct.
Gowri shankar said:
1 decade ago
Seenu your answer is acceptable, but Khagesh Gupta is correct.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers