<p>How does the compiler know the prototype of sleep function or even printf function, when I did not include any header file in the first place?</p>

<p>Moreover, if I specify <code>sleep(1,1,"xyz")</code> or any arbitrary number of arguments, the compiler still compiles it.
But the strange thing is that gcc is able to find the definition of this function at link time, I don't understand how is this possible, because actual <code>sleep()</code> function takes a single argument only, but our program mentioned three arguments.</p>

<pre><code>/********************************/
int main()
{
 short int i;
 for(i = 0; i&lt;5; i++)
 {
    printf("%d",i);`print("code sample");`
    sleep(1);
 }
 return 0;
}
</code></pre>

[{'answer_id': 68861, 'author': 'Jason Dagit', 'author_id': 5113, 'author_profile': 'https://Stackoverflow.com/users/5113', 'pm_score': 2, 'selected': False, 'text': "<p>C will guess int for unknown types.  So, it probably thinks sleep has this prototype:</p>\n\n<pre><code>int sleep(int);\n</code></pre>\n\n<p>As for giving multiple parameters and linking...I'm not sure.  That does surprise me.  If that really worked, then what happened at run-time?</p>\n"}, {'answer_id': 68874, 'author': 'Ben Collins', 'author_id': 3279, 'author_profile': 'https://Stackoverflow.com/users/3279', 'pm_score': 1, 'selected': False, 'text': '<p>Depends on the compiler, but with gcc (for example, since that\'s the one you referred to), some of the standard (both C and POSIX) functions have builtin "compiler intrinsics".  This means that the compiler library shipped with your compiler (libgcc in this case) contains an implementation of the function.  The compiler will allow an implicit declaration (i.e., using the function without a header), and the linker will find the implementation in the compiler library because you\'re probably using the compiler as a linker front-end.</p>\n\n<p>Try compiling your objects with the \'-c\' flag (compile only, no link), and then link them directly using the linker.  You will find that you get the linker errors you expect.</p>\n\n<p>Alternatively, gcc supports options to disable the use of intrinsics: <code>-fno-builtin</code> or for granular control, <code>-fno-builtin-function</code>.  There are further options that may be useful if you\'re doing something like building a homebrew kernel or some other kind of on-the-metal app.</p>\n'}, {'answer_id': 68928, 'author': 'DGentry', 'author_id': 4761, 'author_profile': 'https://Stackoverflow.com/users/4761', 'pm_score': 3, 'selected': False, 'text': "<p>Lacking a more specific prototype, the compiler will assume that the function returns int and takes whatever number of arguments you provide.</p>\n\n<p>Depending on the CPU architecture arguments can be passed in registers (for example, a0 through a3 on MIPS) or by pushing them onto the stack as in the original x86 calling convention. In either case, passing extra arguments is harmless. The called function won't use the registers passed in nor reference the extra arguments on the stack, but nothing bad happens.</p>\n\n<p>Passing in fewer arguments is more problematic. The called function will use whatever garbage happened to be in the appropriate register or stack location, and hijinks may ensue.</p>\n"}, {'answer_id': 69147, 'author': 'Ben Combee', 'author_id': 1323, 'author_profile': 'https://Stackoverflow.com/users/1323', 'pm_score': 3, 'selected': False, 'text': '<p>In classic C, you don\'t need a prototype to call a function.  The compiler will infer that the function returns an int and takes a unknown number of parameters.  This may work on some architectures, but it will fail if the function returns something other than int, like a structure, or if there are any parameter conversions.</p>\n\n<p>In your example, sleep is seen and the compiler assumes a prototype like</p>\n\n<pre><code>int sleep();\n</code></pre>\n\n<p>Note that the argument list is empty.  In C, this is NOT the same as void.  This actually means "unknown".  If you were writing K&amp;R C code, you could have unknown parameters through code like</p>\n\n<pre><code>int sleep(t)\nint t;\n{\n   /* do something with t */\n}\n</code></pre>\n\n<p>This is all dangerous, especially on some embedded chips where the way parameters are passed for a unprototyped function differs from one with a prototype.</p>\n\n<p>Note: prototypes aren\'t needed for linking.  Usually, the linker automatically links with a C runtime library like glibc on Linux.  The association between your use of sleep and the code that implements it happens at link time long after the source code has been processed.</p>\n\n<p>I\'d suggest that you use the feature of your compiler to require prototypes to avoid problems like this.  With GCC, it\'s the -Wstrict-prototypes command line argument.  In the CodeWarrior tools, it was the "Require Prototypes" flag in the C/C++ Compiler panel.</p>\n'}, {'answer_id': 69158, 'author': 'Allan Wind', 'author_id': 9706, 'author_profile': 'https://Stackoverflow.com/users/9706', 'pm_score': 1, 'selected': False, 'text': '<p>In a non-toy example another file may include the one you missed. Reviewing the output from the pre-processor is a nice way to see what you end up with compiling.</p>\n'}, {'answer_id': 69280, 'author': 'vrdhn', 'author_id': 414441, 'author_profile': 'https://Stackoverflow.com/users/414441', 'pm_score': 2, 'selected': False, 'text': "<p>This is to do with something called 'K &amp; R C' and 'ANSI C'.\nIn good old K &amp; R C, if something is not declared, it is assumed to be int. \nSo any thing that looks like a function call, but not declared as function\nwill automatically take return value of 'int' and argument types depending\non the actuall call.</p>\n\n<p>However people later figured out that this can be very bad sometimes. So \nseveral compilers added warning. C++ made this error. I think gcc has some \nflag ( -ansic or -pedantic? ) , which make this condition an error.</p>\n\n<p>So, In a nutshell, this is historical baggage.</p>\n"}, {'answer_id': 71733, 'author': 'itj', 'author_id': 888, 'author_profile': 'https://Stackoverflow.com/users/888', 'pm_score': 2, 'selected': False, 'text': '<p>Other answers cover the probable mechanics (all guesses as compiler not specified).</p>\n\n<p>The issue that you have is that your compiler and linker have not been set to enable every possible error and warning.  For any new project there is (virtually) no excuse for not doing so.  <em>for legacy projects more excuse - but should strive to enable as many as possible</em></p>\n'}]
['https://Stackoverflow.com/questions/68843', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']