1

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;
}
Dan Fego
  • 13,644
  • 6
  • 48
  • 59
user1180619
  • 338
  • 1
  • 4
  • 11
  • 1
    Sounds like a dupe of: http://stackoverflow.com/questions/4800102/not-including-stdlib-h-does-not-produce-any-compiler-error – mwcz Jan 31 '12 at 15:51
  • 1
    You have to understand the fundamental difference between "if" and "only if". Standard documents usually say, "If you do X, you get Y." That *does not say anything* about what happens if you *don't* do X. – Kerrek SB Jan 31 '12 at 15:51
  • 1
    Generally it's better to avoid using system calls [link](http://stackoverflow.com/questions/900666/system-calls-in-c-and-their-roles-in-programming)
    [link](http://www.gidnetwork.com/b-61.html)
    Calling system in windows is basically the same as running cmd.exe and typing pause.
    – k3oy Jan 31 '12 at 16:07
  • @KerrekSB, in C an header must provide only the declarations and definitions it has to (in C++ an header can provide more). – AProgrammer Jan 31 '12 at 16:40
  • @AProgrammer: Very interesting, thanks! I guess that makes sense, since there are no namespaces and you don't want to permit silent pollution with names you didn't expect. – Kerrek SB Jan 31 '12 at 16:47
  • @KerrekSB, I think it also has to do with template definitions dragging in implementation details. – AProgrammer Jan 31 '12 at 17:01
  • As an aside, `system` considered harmful... – R.. GitHub STOP HELPING ICE Jan 31 '12 at 18:31

4 Answers4

11

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.

FatalError
  • 52,695
  • 14
  • 99
  • 116
0

According to the manpages, it is in stdlib.h

C--
  • 16,393
  • 6
  • 53
  • 60
0

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

njsg
  • 125
  • 8
0

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

     #include <stdlib.h>
     int system(const char *string);
ipc
  • 8,045
  • 29
  • 33