C Programming - Command Line Arguments - Discussion
Discussion Forum : Command Line Arguments - Find Output of Program (Q.No. 12)
12.
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample Jan Feb Mar
cmd> sample Jan Feb Mar
/* sample.c */
#include<stdio.h>
#include<dos.h>
int main(int arc, char *arv[])
{
int i;
for(i=1; i<_argc; i++)
printf("%s ", _argv[i]);
return 0;
}
Discussion:
17 comments Page 1 of 2.
Ramit Mittal said:
1 decade ago
@Kiran.
Value of argc=4 here. We will also count sample, though it has an index[0].
Value of argc=4 here. We will also count sample, though it has an index[0].
Opkuma said:
4 years ago
I didn't get it, please explain.
Shubh said:
5 years ago
Can someone explain it clearly?
Fatema Saba said:
6 years ago
@Rahul
Even I have the same doubt.
Is _argc and arc are the same and is _argv and arv is the same? Please, anyone, explain it.
Even I have the same doubt.
Is _argc and arc are the same and is _argv and arv is the same? Please, anyone, explain it.
Matrika said:
8 years ago
@Parvathy.
As there are 4 arguments sample, Jan, Feb, March. So argc would be equal to 4. And the printing is done from the index 1 that means it will print the value of argv[1] as Jan, argv[2] as Feb and argv[3] as March. It will mot print sample as sample is present in argv[0].
As there are 4 arguments sample, Jan, Feb, March. So argc would be equal to 4. And the printing is done from the index 1 that means it will print the value of argv[1] as Jan, argv[2] as Feb and argv[3] as March. It will mot print sample as sample is present in argv[0].
Rahul said:
8 years ago
Is ''arc'' and ''_argc'' same?
Dishank said:
9 years ago
@Kiran.
argc[1] is jan only then how can it print feb and march as feb is argc[2] while march is argc[3].
argc[1] is jan only then how can it print feb and march as feb is argc[2] while march is argc[3].
Nitesh said:
1 decade ago
When it goes in the for loop then 3 is not greater than 3. So how does it prints March because their is no < = sign.
Parvathy said:
1 decade ago
@Ramit.
How can argc be equal to 4? An array always starts with 0 unless specified otherwise. So argc=3 and rest is as @Kiran said.
How can argc be equal to 4? An array always starts with 0 unless specified otherwise. So argc=3 and rest is as @Kiran said.
Anand said:
1 decade ago
_argc, _argv are gloabal variables supported by Turbo C, so if you want to use include the header file #include <dos.h>.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers