Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C/C++ have header files unlike other languages like C# and Java? [duplicate]

Tags:

java

c++

c

c#

I'm interested in the motivation - what was the motivation in C/C++ to have the interface separated from the imlpementation by header files and why didn't the authors of other languages borrow the concept and use it in other(newer) languages? Does it mean that this concept is bad?

EDIT: I'm NOT asking why c/c++ use header files - I'm asking WHY didn't this concept remain in newer languages like Java etc.

like image 998
Novellizator Avatar asked Dec 13 '25 23:12

Novellizator


1 Answers

When the C language was designed computers had very few resources, and it was important to keep the processing time and memory usage to a minimum. A design that would enable a compiler to read the source file and compile it "on the fly", in a single pass, with minimal resource usage, was preferred to designs that made the compiler either do multiple passes over the source code or construct a large data structure in memory before emitting compiled code. Header files enable compiling in a single pass: they give you a way to declare and use a symbol while its definition may come later in the source code, or it may even be in a different file or external library.

The design for newer languages such as C# and Java considered programmer convenience more important than optimizing the compiler's resource usage. Also many modern C compilers already use multiple passes because it's impossible to apply many code optimizations in a single pass; using a single-pass design for new languages has little benefit nowadays.

Header files also have other benefits: they allow you to separate interface from implementation, so that the header file serves as an API documentation without exposing implementation details.

like image 168
Joni Avatar answered Dec 16 '25 14:12

Joni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!