C Programming - Command Line Arguments - Discussion
Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 9)
9.
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
cmd> myprog friday tuesday sunday
/* myprog.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
printf("%c", *++argv[1]);
return 0;
}
Discussion:
12 comments Page 1 of 2.
Vishal kumbhar said:
1 decade ago
Guys,
argv[1] means "friay"
++arv[1] means "r"
it prints "r".
ok
argv[1] means "friay"
++arv[1] means "r"
it prints "r".
ok
Sunitha said:
1 decade ago
argc converts first string datatype to integer hence the string myprog converted to integer and argc reads no of arguments that passed assigneed that values to argv hence pointer points to to string friday's first character and its get incremented and points to r.
Pradeep said:
1 decade ago
Can,any one explain wat is difference between argc and argv?and wat operation it performs in program?with example....
Raut said:
1 decade ago
@pradeep
int main(int argc,char *argv[])
here argc is total no argument you have passed through
command line argument and argv is an array of pointers which hold all pointers to the arguments which you have passed.
for ex my prog.c is a source file. after compilation you got myprog.exe file(lets say if you dont use other file name then you will get ./a.out file by default).
now we type myprog.exe apple orange
then argc=3
argv[0]=myprog.exe
argv[1]=apple
argv[2]=orange.
regards
int main(int argc,char *argv[])
here argc is total no argument you have passed through
command line argument and argv is an array of pointers which hold all pointers to the arguments which you have passed.
for ex my prog.c is a source file. after compilation you got myprog.exe file(lets say if you dont use other file name then you will get ./a.out file by default).
now we type myprog.exe apple orange
then argc=3
argv[0]=myprog.exe
argv[1]=apple
argv[2]=orange.
regards
KomPri said:
1 decade ago
Hi guys....
++argv=friday & ++argv[1]=r
so the output is correct
++argv=friday & ++argv[1]=r
so the output is correct
Ashutosh said:
1 decade ago
argv[1] is the base address of friday. And on incrementing the base address by will point to r because arg[] is a character pointer.
Sanjoy said:
1 decade ago
argv[0] means " myprog ".
argv[1] indicates "Friday".
++argv[1] means = "riday".
It just point to the next character of f .
Here %c is given so only one character "r".
Will print if there were %s then "riday".
Would print,
printf("%c", *++argv[1]);
It prints "r".
argv[1] indicates "Friday".
++argv[1] means = "riday".
It just point to the next character of f .
Here %c is given so only one character "r".
Will print if there were %s then "riday".
Would print,
printf("%c", *++argv[1]);
It prints "r".
Sunaina said:
1 decade ago
Whats the difference between *++argv and **+argv?
Dfds said:
1 decade ago
argv[1] means Friday.
When we increment pointer by one it will point to r.
When we increment pointer by one it will point to r.
Sudha said:
9 years ago
Friends.
The array has a continue memory allocation also. The array index start with zero is going on.
So the Friday store in array memory just like that start with zero.
So f contain a a[0]=f; a[1]=r; a[2]=i, a[_]=y; so the print "r";
Friends please justify the answer.
The array has a continue memory allocation also. The array index start with zero is going on.
So the Friday store in array memory just like that start with zero.
So f contain a a[0]=f; a[1]=r; a[2]=i, a[_]=y; so the print "r";
Friends please justify the answer.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers