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.
Apurva Nigam said:
1 decade ago
But then what arc and *arv[] will contain??, _argc and _argv value respectively ?
And what if we dont pass "int arc, char *arv[]" in main function that is we write only main(), will global variables _argc and _argv still have cmd line arguments value and v can use it ?
In short I want to ask will it work if we write like this:-
#include<stdio.h>
#include<dos.h>
int main()
{
int i;
for(i=1; i<_argc; i++)
printf("%s ", _argv[i]);
return 0;
}
And what if we dont pass "int arc, char *arv[]" in main function that is we write only main(), will global variables _argc and _argv still have cmd line arguments value and v can use it ?
In short I want to ask will it work if we write like this:-
#include<stdio.h>
#include<dos.h>
int main()
{
int i;
for(i=1; i<_argc; i++)
printf("%s ", _argv[i]);
return 0;
}
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].
Kiran raj said:
1 decade ago
cmd>sample Jan Feb Mar
argc=3
argv[]=sample Jan Feb Mar
argv[0]=sample
argv[1]=Jan
argv[2]=Feb
argv[3]=Mar
in prog,
for(i=1;i<_argc; i++)
printf("%s ", _argv[i]);
The o/p printing from argv[1] so,
We will get
jan feb mar
argc=3
argv[]=sample Jan Feb Mar
argv[0]=sample
argv[1]=Jan
argv[2]=Feb
argv[3]=Mar
in prog,
for(i=1;i<_argc; i++)
printf("%s ", _argv[i]);
The o/p printing from argv[1] so,
We will get
jan feb mar
Girish nischel said:
1 decade ago
@sonam.@apurva
I believe that it wont work. better check it on an idea..
n my doubt is here int arc is an integer argument! right!! so what is its puprpose actually!! i m a rookie in 'C',so pls explain me in detali?! or atleats mail me
I believe that it wont work. better check it on an idea..
n my doubt is here int arc is an integer argument! right!! so what is its puprpose actually!! i m a rookie in 'C',so pls explain me in detali?! or atleats mail me
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.
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.
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>.
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.
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].
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].
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers