user4149729
user4149729

Reputation:

Does the system() function belong to C or C++?

#include <stdio.h>
#include <stdlib.h>
void main ()
{
   system("dir");
}

I have read in many C++ books where system() is used to execute command line programs. But when I tried the same command in C, it worked perfectly. So my question is whether its a standard C or C++ function? Please be liberal as I am new to C programming.

Upvotes: 3

Views: 1207

Answers (5)

user4175266
user4175266

Reputation:

Both c and cpp support the function system as they have support for the stdlib.h that contains the prototype of system() function.

Upvotes: 0

Mike Seymour
Mike Seymour

Reputation: 254461

Both: it's defined by the standard C library, and the standard C++ library contains the C library.

In C++, you should include the C++ style header, <cstdlib>, and refer to it as std::system. Including C headers directly is deprecated.

Upvotes: 4

As system function is declared in stdlib.h, it can be considered a C function. But in C++, stdlib.h is merged into the std namespace and is located in the cstdlib include in this form. So the correct answer is "both".

Upvotes: 5

user743382
user743382

Reputation:

It's both. C defines many functions. C++ defines many functions that are exactly the same as in C, some that are subtly different from how they are in C, and a lot of functions and classes that aren't part of C at all. Knowing that a function is part of standard C++ says nothing about whether it is part of standard C, and knowing that a function is part of standard C says little about whether it is part of standard C++.

Upvotes: 3

Drax
Drax

Reputation: 13278

It is both C and C++.

Upvotes: 4

Related Questions