1

Is it necessary to include dos.h header file in a c program while using _argc and _argv global variables?

Like in the following example:-

/* sample.c */
#include<stdio.h>
#include<dos.h>
int main()
    {
        int i;
        for(i=1; i<_argc; i++)
            printf("%s ", _argv[i]);
        return 0;
    }

if yes what is the significance of including dos.h?

Ignorant
  • 19
  • 3

3 Answers3

1

If you meant to use the programs arguments, the standard way looks something like this:

int main(int argc, **argv)
{
/* use argc and argv here */
}

These are not global, but you can copy them to global variables. If those _argc and _argv mean something else, well, then dos is very confusing.

Gábor Buella
  • 1,840
  • 14
  • 22
  • 1
    Also, dos.h is not at all needed for this. Maybe for _argc, or _argv, whatever those are under dos, but not for argc and argv. You can try those in dos anyways, and see if t works there. – Gábor Buella Mar 20 '14 at 20:37
1

Turbo C had this variables, you can read more at:

http://www.indiabix.com/c-programming/command-line-arguments/discussion-477

and

http://webcache.googleusercontent.com/search?q=cache:NEjvrTslYvEJ:www.codeforge.com/read/93772/DOS.H__html

so yes, if you need to use those variables include dos.h... otherwise just use a standard main, like in the other answers.

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
0

The direct answer to your question is that the variables have to be declared somewhere for the compiler to find them. In dos.h, or some file included in dos.h, there is a definition for _argc and _argv.

If you didn't include dos.h, you would still somewhere have to declare extern int _argc; and extern char *_argv[]; to be able to use them. Otherwise you'll get a compiler error because the compiler won't have any idea what these variables are.

These variables are probably provided by your C runtime. When you build a program, your compiler and linker put in all sorts of supporting boilerplate code involved with starting your app. If you're wondering where these variables actually exist, it's in this boilerplate startup code.

(This startup code is what gets run when your program launches. The startup code then hands off control to your program by calling your main() function.)

For this specific case, though, you really want to use the argc and argv provided to main() as defined by the C standard. That would make your program portable to all C compilers and not limited to your specific (probably ancient) compiler. Quoth the standard,

The function called at program startup is named main . The implementation declares no prototype for this function. It can be defined with no parameters:

     int main(void) { /*...*/ }

or with two parameters (referred to here as argc and argv , though any names may be used, as they are local to the function in which they are declared):

     int main(int argc, char *argv[]) { /*...*/ }

And of course if you use these argc and argv, you certainly don't need dos.h.

indiv
  • 17,306
  • 6
  • 61
  • 82
  • Note for when you quote the standard: please remember to say which version of the standard you are quoting and which paragraph in which section, so others can easily navigate to it. – Shahbaz Mar 20 '14 at 23:10
  • I found it useful. Thanks – Ignorant Mar 21 '14 at 11:25