user1180619
user1180619

Reputation: 348

system () in stdio or stdlib?

I used the system("pause") with stdio.h and it worked without error. When I looked at the stdio functions, system() is in stdlib. How come it worked, and here is the code?

#include <stdio.h>

int main() {
    printf("Hello World\n" );
    system("pause");
    return 0;
}

Upvotes: 1

Views: 5801

Answers (4)

njsg
njsg

Reputation: 125

#include only carries the function declaration (prototype), the functionality is provided by a library, that's included in the linking stage.

Even if you do not #include it, as far as the definition assumed by the compiler when compiling matches the one in the library carrying it, there is no error and it will work.

Upvotes: 0

ipc
ipc

Reputation: 8143

From the Standard at 4.10.4.5 The system function is in stdlib.h:

     #include <stdlib.h>
     int system(const char *string);

Upvotes: 0

C--
C--

Reputation: 16558

According to the manpages, it is in stdlib.h

Upvotes: 0

FatalError
FatalError

Reputation: 54551

The answer is that it's an implicit declaration. If the compiler doesn't see a prototype for a function, it assumes it was declared like:

int system();

If you turn up the warning level on your compiler, you'll likely see that this causes a warning. Implicit declarations are generally undesirable, but in this case it's why this works without causing any errors.

Upvotes: 11

Related Questions