tags:

views:

62

answers:

3

what exactly is a preprocessor directive .?? i do have an idea that #include is a preprocessor directive but what does it exactly do ..??

+1  A: 

As the name implies the PRE processor does processing on source files before it goes to the compiler. The C language has a preprocessor that does various things.

#include - imports and expands a file into the file to be compiled.
#define - macros that are changed into strings in the file that is to be compiled.

Wikipedia (I know guys people hate this) has a good overview for the C preprocessor.

There is nothing that stops you from using another one such as m4 or writing your own, which would modify or add strings into file based on certain template notation.

Romain Hippeau
A: 

A preprocessor is an application which processes something before the main processing.

In this context, the preprocessor prepares the source code before the compiler works on it to generate code. The preprocessor handles removing comments, expanding #defines, and #including files, among other thiings.

The main principle behind all it preprocessor tasks, is that the PP knows nothing about the syntax of the programming language. It's just doing mindless text manipulation.

James Curran
A: 

The preprocessors you encounter in languages like C or C++, is basically an automatic text manipulator. It manipulates the source code, so to say.

A directive is a command to control that preprocessor.

For instance: #include <stdio.h> means stdio.h should be copied in this place in your current file.

What's important to note, is that the preprocessor only works on a textual level. It copies or inserts text, or deletes it, but it doesn't do any translation into Assembly, nor does it do any linking or compiling further down the line.

In order not to copy text multiple times over, you'd often see this construct in header files:

#ifndef _HEADERFILE_H_
#define _HEADERFILE_H_

/* rest of headerfile... */

#endif

This ensures, the headerfile is copied only once into the complete code that is to be compiled after preprocessing (since you may have multiple .c files with #include "headerfile.h" in it.).

All preprocessor directives are prefixed with # when using the preprocessor in gcc for C or C++. Depending on compiler suite and programming language, this may be different, though.

polemon
That reminds me, I'd like to have a "generic" preprocessor, one that works on any kind of programming language, would be great for developing new IDEs or when designing your own programming language.Is there something like this already?My Point is, there's no preprocessor for Java, and I sometimes miss the ability to modify the source code automatically when compiling.
polemon
^^ I agree, I think a lot of work needs to be done in terms of preprocessing technology in terms of getting user scripts into into the compile stages.
Tomas Cokis