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.
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
A$tro said:
9 years ago
This program will print an output on both environment Linux/windows-and I think we can define an array size using either variable/macros/const.
As I've tried to define the max as shown:
int max =20;
C Const int max=20; ------------- and it works on both cases.
So may be the error depends on the standard ANSCI CS99 etc.
As I've tried to define the max as shown:
int max =20;
C Const int max=20; ------------- and it works on both cases.
So may be the error depends on the standard ANSCI CS99 etc.
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".
Sudha said:
10 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.
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.
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.
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....
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
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.
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers