#include directive – Preprocessor for header inclusion
Understanding the purpose of include preprocessor
Learning Objectives
- Observing specific pre-processor directives #include for inclusion of header files relevant to function and objects we are going to use in our program.
Source Code
|
Run Output
Code Understanding
#include <iostream>
This include is there to use iostream function and objects like cout, cin, end etc.
#include <math.h>
This pre-processor include directive is for including the math related functions in this program. This helps is using functions like sqrt(), sin(), cos(), pow() etc.
using namespace std;
int main()
{
cout<<sqrt(25)<<endl;
Here we have been able to use sqrt function because math.h is included.
return 0;
}
Notes
- iostream inclusion is most common in C++ programs. Some compilers include them by default.
- some compiler may allow only modern version header files. Like only <iostream> may be allowed in place of <iostream.h> (The older version).
- The latest version header file for math functions is <cmath> instead of older version <math.h>
Common Errors
- Forgetting relevant inclusion is most common error.
- Sometimes a program may compile on one compiler but not on another one. This may be due to inclusion differences. For e.g. strlen() may work only when string,h is included, but on some compiler it may be included by default.
Suggested Filename(s): pp-incl.cpp, include-directive.cpp
sunmitra| Created: 1-Sep-2018 | Updated: 19-Nov-2018|