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
/* 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;
}
No output
sample Jan Feb Mar
Jan Feb Mar
Error
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
17 comments Page 1 of 2.

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>.

Karthigeyan said:   1 decade ago
_arc, _arv are used as argc,argv

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;
}

Sonam said:   1 decade ago
Good doubt apoorva ! even I have the same doubt can some one clear it ?

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

Khushi said:   1 decade ago
Please anyone explain this program in detail.

Lijina said:   1 decade ago
Please any one explain it clear.

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

Ramit Mittal said:   1 decade ago
@Kiran.

Value of argc=4 here. We will also count sample, though it has an index[0].

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.


Post your comments here:

Your comments will be displayed after verification.