C++/hi

Advertisement


Question
#include <iostream>
#include <cctype>
#include <iomanip>
#include <cstdlib>
can you explain what each of these mean and how can i remember to type it when writing a program?like how can i remember it easily without looking it up or on a paper?

Answer
#include is a preprocessor directive.

As the name implies, the preprocessor operates on the source code before the compilation begins. The preprocessor performs a pass over the text of the source file, tranforming it various ways based on the preprocessor directives it encounters. It then presents the altered source code to the compiler; the compiler itself is entirely unaware of the transformational preprocessor directives.
For more information, see: http://en.wikipedia.org/wiki/C_preprocessor

Of the preprocessor directives, probably the most important (and the first one that you encounter) is the #include directive. The directive

#include "path/filename"

or

#include <path/filename>

will cause the text of filename to be inserted into the source stream at the point where the directive occurs.

For example, given the file foo.h,

/* foo.h */

extern int bar;

float foo();

which is then used in file foo.cc

/* foo.cc */

float baz = 3;

#include "foo.h"

int bar = 10;

float foo()
{
 return bar * baz; /* multiply the current values of bar and baz */
}

the preprocessor would produce:

float baz = 3;

extern int bar;

float foo();

int bar = 10;

float foo()
{
  return bar * baz;
}

In C, header files are usually given the extension .h. In C++, the standard headers do not have an extension at all. The C headers when used in C++, have the letter 'c' preprepended to the header name (i.e., <stdlib.h> becomes <cstdlib>).

The locations where the preprocessor searches for the header files is determined by how the path is demarked; if the file path is in angle brackets <>, then the preprocessor searches only the compiler-defined library include paths, whereas if it is in double quotes "", then it searches the paths containing user headers first.

Consider the following program:

#include <iostream>

int main()
{
   using namespace std;
   std::cout << "Hello, world!\n" ;
}

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

> how can i remember it easily without looking it up or on a paper?

As you become familiar with writing programs, you will discover that remembering this is not difficult. In the mean-time, you could just write out the #includes that you most often need into a file - for example, a file called "std_headers.h"

file std_headers.h
------------------

#include <iostream>
#include <cctype>
#include <iomanip>
#include <cstdlib>

And then, copy the file to the same directory containing your source program which has just one directive:

#include "std_headers.h"

Or alternatively, just copy this file to the beginning of every program that you write.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.