-1

When we add the header files to our program, the main intent is that we have to use their functions and, to use any function it is compiler's job to clarify that it does not have any error. So when we compile our program, does compiler executes all the functions that are written in the included header files?

Pranav
  • 105
  • 5
  • 1
    Can you please share you what you mean by "does the compiler execute all the functions"? – François Andrieux Aug 12 '20 at 21:42
  • Also don't tag multiple programming language tags. – Geno C Aug 12 '20 at 21:43
  • Header files don't generally include "instructions", they include lots and lost of *declarations*. – tadman Aug 12 '20 at 21:43
  • 1
    Compilers don't *execute* the code they compile, they *compile* it. Some compilers do offer the capability of [precompiled headers](https://stackoverflow.com/q/903228/10077). – Fred Larson Aug 12 '20 at 21:44
  • Tagging this C and C++ will cause a lot of flack. C++ may have a lot of functional overlap with C, but it is a different language and entirely different rules apply. Additionally, in the future C++ will have modules and C will likely not. – tadman Aug 12 '20 at 21:44
  • Also a related question: [Are there any performance implications to including every header?](https://stackoverflow.com/q/33698185/10077) – Fred Larson Aug 12 '20 at 21:48
  • The compiler converts the text source files, like the headers and cpp files, into executable binary assembler code. It does not execute the source code in any way. If there are errors in the source code it will fail to do this conversion. – Ian4264 Aug 12 '20 at 21:54

2 Answers2

0

Think of header files as blueprints and instructions on how to use various data structures and functions. Without these declarations the compiler does not know how to compose a function call correctly, or what the memory layout of a given structure is.

These are separated from the implementation because these definitions are typically shared among different source files that need to make use of them.

They are not "instructions" in the machine instruction sense, they do not execute any code. C is not like a scripting language where actual code gets executed during the compilation phase. Instead the compiler converts the code into an executable that, unless executed, doesn't actually do anything, it's just a file.

Header files were a consequence of how limited computers were when languages like C were designed and implemented. C was developed in the early 1970s when a very expensive machine like a PDP-11 had around 4MB of memory total, and that was often shared among multiple users.

Today languages like Rust and Swift have eliminated the need for header files. They just parse the source and extract the declarations as necessary. This is only possible because computers have several thousand times more memory.

tadman
  • 208,517
  • 23
  • 234
  • 262
0

When we add the header files to our program, the main intent is that we have to use their functions and, to use any function it is compiler's job to clarify that it does not have any error.

When we #include a header in our source file, the main intent is that the contents of the header be treated as if they appeared, at that point, directly in the source file. The compiler has the same responsibilities toward the resulting aggregate translation unit as it has toward a translation unit that does not #include anything, including identifying and diagnosing violations of language constraints.

The conventional use of header files in C is to provide declarations of functions and variables defined elsewhere, and to provide definitions of types and macros that may be useful. C header files typically do not include function or variable definitions (as opposed to declarations), as that is troublesome.

C++ header files serve a similar purpose to C header files, but they often also include inline function definitions, especially of class constructors, destructors, and member functions.

Again, however, all but the first paragraph of that is conventional use, not language rule. It turns out to be very useful to have headers containing reusable declarations (and inline function definitions) so that we don't have to know or type all of the required declarations of all of the functions and external objects we want to use, but we could, in principle, write the needed declarations by hand into every source file without changing the meaning of the overall code.

So when we compile our program, does compiler executes all the functions that are written in the included header files?

No, why would it? The compiler compiles source code, which includes headers, to executable programs and libraries. That does not involve executing anything defined in the program being compiled, regardless of whether it is defined in a header. Functions are executed when one of the resulting programs is run.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157