ant2009
ant2009

Reputation: 22486

#ifdef #else #endif macro question

I am new to C, and I am maintaining someones code. I came across this in the header file. I can understand that if the source is compiled on the windows it will enter the if statement else if the code is compiled on a linux it will enter the else statement. Correct me if I am wrong.

However, the question is why is # (hash) used in front of all the include headers?

Many thanks for any suggestions,

#ifdef WIN32
#   include <conio.h>
#   include <process.h>
#   include <stdlib.h>
#   include <string.h>
#else
#   include <unistd.h>
#   include <termio.h>
#   include <sys/types.h>
#   include <sys/stat.h>
#   include <fcntl.h>
#endif

Upvotes: 2

Views: 4388

Answers (6)

lavinio
lavinio

Reputation: 24299

The # lines are actually handled not by the C compiler itself, but by a preprocessor that runs as an early stage in the compilation pipeline. The "#" is how it knows which lines it is responsible for.

That same preprocessor can be used in other contexts as well.

The preprocessor can not only do evaluation of expression, as in the #if and #ifdef clauses, but it can also open other files and insert them using #include and even do text substitution using #define clauses.

More information can be found in the Wikipedia entry on the C preprocessor.

#include is different from, say, the VB.Net Imports statement or the C# using statement. Those make references to other classes, but #include actually inserts the text of the included file at that location in the source file. And it can act recursively, so that included files may themselves #include still others.

Upvotes: 3

abelenky
abelenky

Reputation: 64672

#include is the way you include files in C.

You might be confused by the spaces between the # and the include.

But they don't matter. These lines are still #include.

Upvotes: 7

James Eichele
James Eichele

Reputation: 119106

include, ifdef, etc. Are all preprocessor directives, so they must have the pound (or hash) character in front of them. The coder who wrote this code simply lined up all of those # characters on the left side to make to code look cleaner (in his opinion).

cplusplus.com has a good overview of preprocessor directives.

Upvotes: 1

Kane Wallmann
Kane Wallmann

Reputation: 2322

The #include directive tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears.

http://msdn.microsoft.com/en-us/library/36k2cdd4(VS.80).aspx

Upvotes: 1

Tom Dalling
Tom Dalling

Reputation: 24125

The hash (#) indicates a preprocessor directive. The preprocessor runs over the code before compilation and does things depending on all the lines beginning with "#". The "#include filename.h" directive essentially copies all the contents of filename.h and pastes it where the "#include filename.h" line was.

Upvotes: 8

jsight
jsight

Reputation: 28409

Because "#include" is the syntax for tell the preprocessor to include a header. The spaces after the pound are just there for formatting and are not strictly necessary.

Upvotes: 3

Related Questions