SilverScreen Solid Modeler

Preprocessing Directives

Preprocessing Directives

Previous topic Next topic  

Preprocessing Directives

Previous topic Next topic JavaScript is required for the print function  

SilverCEllipse

 

Preprocessing Directives

 

 


The table below shows  the preprocessor commands and operators that are supported in SilverC. These are further explained in sections that follow.

 

Command

Meaning

#include

Include text from another source file

#define

Define a preprocessor macro

#undef

Undefine a preprocessor macro

#if

Conditionally include ensuing text, based on the value of a constant expression

#ifdef

Conditionally include ensuing text, based on whether a preprocessor name is defined

#ifndef

Conditionally include ensuing text, based on whether a preprocessor name is not defined

#else

Conditionally include ensuing text, if previous #if , #ifdef , #endif or #elif failed

#elif

Conditionally include ensuing text, based on the value of a constant expression, and if previous #if , #ifdef , #endif or #elif failed

#endif

Terminate conditional text inclusion

#error

Force compiler to issue an error

#pragma

Specify implementation defined information to the compiler

#line

Cause compiler to believe that the current line, and possibly current filename, are as specified

defined

Preprocessor operator that yields 1 if its operand is a preprocessor name, and 0 otherwise

#

stringizing operator: replace a macro parameter with a string containing the associated argument

##

token-pasting operator: concatenate two adjacent tokens to form a single token

 

 


Include Directives

The #include directive is used to cause C source to be read in and inserted at that point in the compilation. Standard C defines three forms of #include ; SilverC implements only the first two:

 

#include < h-char-sequence >

h-char-sequence is any sequence of characters except > and newline

#include " q-char-sequence "

h-char-sequence is any sequence of characters except " and newline

#include token-sequence

token-sequence is any sequence of non-whitespace characters not described above

Note that the SilverC compiler does not distinguish between the "" form and the <> form in terms of the places where it searches for files.

 

 


Predefined Macros

SilverC predefines the following macros before beginning compilation:

 

Macro

Definition

__TIME__

Current time as a string, for example, "14:23:32"

__DATE__

Current date as a string, for example, "Jan 20 2010"

__FILE__

Current source filename

__LINE__

Current line in current source file

__STDC__

defined, but set to 0 since SilverC does not conform fully to Standard C

_SILVERC

SilverC version number times 100, e.g. 906 denotes version 9.06

 

These macros are not allowed to be modified (as by #undef , #define sequences). All of them but __FILE__ and __LINE__ remain constant during compilation; __FILE__ and __LINE__ are not true constants, since they change as compilation proceeds. Both may be affected by use of the #line directive.

 


Stringizing

The SilverC preprocessor supports the # operator (the 'stringize' operator), which is used to convert macro parameters into strings. An occurrence of # in a macro definition, followed by the name of a macro formal parameter causes the # and the parameter to be replaced by the corresponding actual argument, enclosed in quotation marks. Each sequence whitespace that occurs in the expansion of the formal parameter is replaced by a single space. Embedded quotation or backslash characters are preserved.

 

It is an error for the # operator to occur without being followed by the name of a macro formal parameter.

 

An example:

 

 #define Stringize(x) # x

   ...

 char msg[60];

 

 strcpy( msg, Stringize( 1 + 2 = 3 ) );

 

The array msg now contains the string "1 + 2 = 3".

 

 


Token Pasting

The SilverC preprocessor supports the ## operator (the 'token pasting' operator), which is used to combine the two tokens surrounding the ## operator into a single token. This process may yield an invalid token, in which case, a compile error will be generated.

 

It is an error for the ## operator to occur without being preceded and followed valid C tokens.

 

An example:

 

 #define MAKE_HANDLE(name) typedef struct name##_t { int dummy; } *name

 MAKE_HANDLE(Screen);

 MAKE_HANDLE(Window);

 

 void DestroyScreen( Screen *screen )

 {

 ...

 }

 

 void DestroyWindow( Window *window )

 {

 ...

 }

 

 
This example uses the token pasting facility to implement type-checked handles. A handle is an opaque data type that stands in for some other data structure, often used to provide a safe interface to a hidden implementation (the handles are usually converted by the implementation to the real type).

 

Traditionally in C, ints or void pointers (void *) are used to implement handles. The drawback to this approach is that handles to differing types are indistinguishable, opening the door to type violations undetectable by the compiler.

 

In this example, the macro MAKE_HANDLE uses the token pasting facility to fabricate unique types based on a dummy structure; the handles are pointers to these types. These pointers are type-safe in that the compiler can distinguish between them in calls and assignments. The effect of the two uses of MAKE_HANDLE is as if the following code were written:

 

 

 typedef struct Screen_t { int dummy; } *Screen;

 typedef struct Window_t { int dummy; } *Window;

 

 
The Screen_t and Window_t types are then used in the interface to routines to destroy screens and windows, wherein it would probably be disastrous to pass a screen handle to DestroyWindow , or a window handle to DestroyScreen .

 

This type of usage gives the compiler a chance to catch such invalid usages.

 

 


#line Directive

The #line preprocessor directive may be used to cause the compiler to believe that the current line, and possibly the current filename are different than they really are. The syntax is either of:

 

 

 #line <integer constant> "filename"

 #line <integer constant>

 

 
This obscure construct is normally used by programs that generate C programs, and is provided for completeness sake. Contrary to Standard C, macros are not expanded when SilverC processes such a line.

 

 


#error Directive

The #error preprocessor directive may be used to force a compiler error to be generated, that is, compilation will not complete successfully. The syntax is as follows:

 

 

 #error <token sequence>

 

 
will cause the following compiler error to be generated:

 

    E088: <filename> <line #>: #error: <token sequence>

 

For example:

 

 #if ! defined(_SILVERC)

 #error This must be compiled by SilverC!

 #endif