Monday, June 8, 2009

Including a Header Files Only Once.................



To prevent multiple copy of header file, a header file can be
written to detect whether it has already been included. The
following is an example of how this can be done:
/* header.h */
#ifndef HEADER_H
#define HEADER_H
/* The body of the header file */
#endif /* HEADER_H */
In this example, the header file is named header.h. The first
line tests whether HEADER_H has been defined. If it has, the
entire header file is skipped. If HEADER_H has not been defi-
ned, it is immediately defined and the header file is proces-
sed. The system header files all use this technique. The names
defined in them all begin with an underscore character to
prevent them from interfering with any names you define. The
convention is for the defined name to be in all uppercase and
to contain the name of the file.


Example:
=============================================================
/*test.h*/

#ifndef _TEST_H /*if not defined */
#define _TEST_H /*then define */

class test
{
private:
int data;
public:
test();
void functionOne();

};

test::test()
{
cout < < "i got nothing to do here" <
< endl;
}

void test::functionOne()
{
cout < < " I m in function One" < <
endl;
}
#endif /*End of definition */
____________________________________________________________

/* main.cpp */

#include "test.h"
#include "test.h"
#include "test.h"

#include < iostream >

using namespace std;


int main()
{
test obj;
obj.functionOne();
return 0;
}
=============================================================

Now you can see the work of only preprosessor by using the
compilation option:
g++ -E main.cpp < output.txt

Now an output file "output.txt" will be created which
contains all the source code after the work of preprocessor
has been done. At the begining it can be seen that the test.h
is defined only once. then after this iostream is defined and
at last source code of main.cpp




Sunday, June 7, 2009

About Macrossss...................

A macro definition is contained on one line. If we need to write it on multiple
lines because of its length, we can do so by using the backslash as
a line continuation character
Example:
#define ran(low,high) \
((int)random() % (high-low+1)) \
+ low

========================================================

To change the definition of a macro, it is necessary to delete it and define it
again.
Example:

#define MLKEYVAL 889
#undef MLKEYVAL
#define MLKEYVAL 890
=========================================================

For a macro to be defined as having parameters, there must be no spaces
between the name of the macro and the parentheses.
Example:
#define ADD(a,b) printf("%d\n",a+b)
#define incrint (a) a++

showint(30,10);
incrint(bbls);
following is the result of preprocessing the previous lines:

printf("%d\n",30+10);
(a) a++(bbls)

==========================================================

Macro names are not substituted inside strings, as in the following
Eexample:

#define Gaggu 8192
printf("THE Gaggu KNOWS HOW TO DO.\n");

The output looks like the following:

THE Gaggu KNOWS HOW TO DO.

==========================================================

An argument passed to a macro can be “stringized” by preceding its name with
a hash (#) character. In the following example, the macro named GAGGU contains
a stringized version of its argument, which is combined with other strings (by
being placed adjacent to them):
Example:
#define GAGGU(ZINGALALA) \
printf("The term " #ZINGALALA " is a string\n")

MONCK(KARLOSE);
The output looks like the following:

The term KARLOSE is a string

===============================================================

Tuesday, June 2, 2009

Directives and Preprocessor.....................

Directive

The instructions to the preprocessor appear in the source as directives and
can be easily spotted in the source code because they all begin with a hash (#) character,
appearing as the first non blank character on a line. The hash character usually appears
on column 1 and is immediately followed by the directive keyword

Example
    #define
    #include
    #ifndef

Preprocessor

The preprocessor reads the source code and responds to directives
embedded in it to produce a modified version of the source, which is fed to the
compiler. The preprocessor is still an important part of C, C++,

Macro

The macro has a name that,when found elsewhere in the text, is replaced with the string of characters defined as the value of the macro. It is possible to specify parameters that are to be used as part of the macro expansion.

Example

#define min(a,b) ((a) < (b) ? (a) : (b))















































































Directive
Description

#define


Defines a name as a macro that the preprocessor will expand

#elif


Provides an alternative expression to be evaluated by an #if directive.

#else


Provides an alternative set of code to be compiled if an #if, #ifdef,

or #ifndef is false.

#error


Produces an error message and halts the preprocessor.

#if


Compiles the code between this directive and its matching #endif only

if evaluating an arithmetic expression results in a nonzero value.


#ifdef


Compiles the code between this directive and its matching #endif only

if the named macro has been defined.

#ifndef


Compiles the code between this directive and its matching #endif only

if the named macro has not been defined.

#include


Searches through a list of directories until it finds the named

file; then it inserts the contents of the file just as if it had

been inserted by a text editor.


#include_Next



The same as #include, but this directive begins the search for the file

in the directory following the one in which the current file was found.


#line



Specifies the line number, and possibly the file name, that is reported

to the compiler to be used to create debugging information in the object

file.


#pragma



A standard method of providing additional information that may be

specific to one compiler or one platform.


#undef



Removes a definition previously created by a #define directive.


#warning



Produces a warning message from the preprocessor.


##



The concatenation operator, which can be used inside a macro to combine

two strings into one.

Search Ranjeet's Blog