Header file and library in C

Category: Education

Presentation Description

No description available.


Presentation Transcript

Header file and library in C++:

Header file and library in C++ Few quick slides to understand easily what is a Header file and why we are using them in C programming. (put in simple words for the sake of understanding) By – Prabhu.R

What is Header file?:

What is Header file? In C or C++ we cannot do compilation, without declaring the variables before in the function. Once declared can be used anywhere in the function(C++). In a similar way do we declare the variable cout / cin / int /main etc before we use them? then how compiler does not pop out an error? Answer is YES we declare them at the Beginning of the program (#include blah blah blah ) But most of us don’t know why we insert this everytime in a program.


Cont. This #include <anything> is called the header file , which is set of lines in which all the predefined variables ( cout / cin /while/if etc) are declared already. Since we must have to write the same set of codes in every program, they created it as a header file such that it can be used without typing the entire codes again and again, thereby just replacing the codes of declaration with # include < ---- > That’s all folks this is all about header file. 


Example This program prints “Hello, world!” to the console using cout . However, our program never defines cout , so how does the compiler know what cout is? The answer is that cout has been declared in a header file called “ iostream ”. When we use the line #include < iostream >, we are telling the compiler to locate and then read all the declarations from a header file named “ iostream ”.

What is a Library?:

What is a Library? Like the actual libraries, these libraries holds many volume of declarations. Typically, a library includes a header file that contains declarations for everything the library wishes to expose (make public) to users, and a precompiled object that contains all of the implementation code compiled into machine language . Why are libraries precompiled? 1. since libraries rarely change, they do not need to be recompiled often, if ever. It would be a waste of time 2. because precompiled objects are in machine language, it prevents people from accessing or changing the source code, which is important to businesses or people who don’t want to make their source code available for intellectual property reasons.

Formats :

Formats Header files are in the format of “.h” Libraries are in the format of “.lib”, “. dll ” – windows. “.a”, “.so” - unix . NOTE: We can create our Header files also if we want.



authorStream Live Help