Home > Enterprise >  Why does calling system() without including stdlib.h still work?
Why does calling system() without including stdlib.h still work?

Time:06-09

The only library I've included in my C program is stdio.h. Calling system() within that very same program works anyway, though Eclipse complains about an implicit declaration of function ‘system’ [-Wimplicit-function-declaration], whatever that means.

However, GCC (the compiler I'm using) seems happy. Would it be that Eclipse is automatically fixing the issue before compilation, or is GCC just kind enough to do that without complaining? I understand nothing of this.

I'm using GNU/Linux Debian 11 (Bullseye) Stable, if that makes any difference.

CodePudding user response:

Once upon a time, the rule in C was that if you called a function the compiler had never heard of, it quietly assumed it was an ordinary function returning int.

The system() function fits that description.

In more recent versions of the C Standard, the "implicit int" rule has been removed, and you are required to declare all functions before calling them. A modern compiler is obliged to issue a diagnostic if you fail to declare a called function. However, there's nothing keeping the compiler from having the diagnostic be a nonfatal warning, and going on to compile your program anyway, making use of the old assumption. And in fact, many compilers still do that, perhaps to make it easier to compile old code written under the old rules.

  • Related