Search code examples
cincludeheader-filesdirective

can someone explain me why the #include directive in C allows to include files other that ".h" files


can someone explain to me why writing something like:

#include "file.txt"       
#include "File.js"        /* why including files other than files with .h extension is valid */
#include "anotherFile.c"
main()
{

    printf("why including files other than header files (.h files) is allowed in C");

}

So can someone provide me a good tutorial online describing this. when i search online about this i find nothing. is this behavior of #include directive described somewhere in the C standard pdf or C standard html if so please guide me to the actual page where this behavior is described in the C standard with a link


Solution

  • The include directive is part of the C Preprocessor. The preprocessor will simply replace an include with the contents of the file referenced. See 6.10.2 Source file inclusion:

    A preprocessing directive of the form #include "q-char-sequence" new-line causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters.