简体   繁体   中英

why do C functions work even without importing the libraries they are written in?

int main()
{
    srand(time(0));
    int minimo = rand();
    int generato;

    while(1)
    {
        generato = rand();
        if(generato < minimo)
        {
            minimo = generato;
            printf("%d\n", minimo);
        }
    }
    return 0;
}

this code works perfectly on my computer without importing any library. I just wrote it how you see here and it compiled and ran succesfully with no errors. I'm using a c11 std-GNU compiler

Due to the history of C, some compilers still allow use of functions without declaring them. When an identifier is used to call a function, a default declaration of a function without a prototype (the number and types of parameters are unspecified) return int is provided. If the function does indeed return int , as rand and printf do, and the arguments after default argument promotions match the actual types of the parameters, this works. It may also work if the function does not return anything (has return “type” void , as srand does).

The 1990 C standard said, in clause 6.3.2.2:

… If the expression that precedes the parenthesized argument list in a function call consists solely of an identifier, and if no declaration is visible for this identifier, the identifier is implicitly declared exactly as if, in the innermost block containing the function call, the declaration

extern int identifier ();

appeared.

This was dropped in the 1999 C standard.

I'm using a c11 std-GNU compiler

If you mean you are using GCC, then I would expect it to issue warnings at least, in its default mode or if -std=c11 is used.

You should not use this “feature.” It exists to support old source code, and its acceptance in default compiler settings is questionable.

When I compile this code on my computer, with no special options, I get lots of warnings:

$ cc test.c
test.c: In function ‘main’:
test.c:3:5: warning: implicit declaration of function ‘srand’ [-Wimplicit-function-declaration]
    3 |     srand(time(0));
      |     ^~~~~
test.c:3:11: warning: implicit declaration of function ‘time’ [-Wimplicit-function-declaration]
    3 |     srand(time(0));
      |           ^~~~
test.c:4:18: warning: implicit declaration of function ‘rand’ [-Wimplicit-function-declaration]
    4 |     int minimo = rand();
      |                  ^~~~
test.c:13:13: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
   13 |             printf("%d\n", minimo);
      |             ^~~~~~
test.c:13:13: warning: incompatible implicit declaration of built-in function ‘printf’
test.c:1:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
  +++ |+#include <stdio.h>
    1 | int main()

I suspect you saw similar messages and ignored them because you got an executable anyway. It is very dangerous to do that with C. For historical reasons, a whole lot of diagnostics that should be hard errors are only warnings with most C compilers. Therefore, until you have a lot more experience with the language, you should treat all warning messages as if they were hard errors.

Since it sounds like you're using GCC, I recommend you use this set of options until you get more experience:

    -g -Og -std=gnu11 -Wall -Wextra -Wpedantic -Wstrict-prototypes -Werror

They turn on a bunch more diagnostics that ought to be hard errors but, again for historical reasons, aren't printed at all by default, and promote all of them to hard errors. Adjust the optimization level ( -Ox ) as you see fit, and maybe add more warning options (there's a huge list in the manual) but don't take anything out.

(You need to turn on some level of optimization to get all of the diagnostics. Yes, really.)

(I advise against using the hyperconformant -std=c11 mode until you get more experience; it enables things you actively don't want (like trigraphs) and has a nasty tendency to break the system headers.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM