![]() |
|
| << Previous | Index | Next >> | |
| | |
Dynamic C is based on the C language. The programmer is expected to know programming methodologies and the basic principles of the C language. Dynamic C has its own set of libraries, which include user-callable functions. Please see the Dynamic C Function Reference Manual for detailed descriptions of these API functions. Dynamic C libraries are in source code, allowing the creation of customized libraries.
Before starting on your application, read through the rest of this chapter to review C-language features and understand the differences between standard C and Dynamic C.
4.1 C Language Elements
A Dynamic C program is a set of files consisting of one file with a main() function and the requested library files. Each file is a stream of characters that compose statements in the C language. The language has grammar and syntax, that is, rules for making statements. Syntactic elements, often called tokens, form the basic elements of the C language. Some of these elements are listed in Table 4-1.
4.2 Punctuation Tokens
Punctuation serves as boundaries in C programs. Table 4-2 lists the punctuation tokens.
4.3 Data
Data (variables and constants) have type, size, structure, and storage class. Basic (a.k.a., primitive) data types are shown below.
4.3.1 Data Type Limits
The following symbolic names for the hardcoded limits of the data types are defined in
limits.h.4.4 Names
Names identify variables, certain constants, arrays, structures, unions, functions, and abstract data types. Names must begin with a letter or an underscore (
_), and thereafter must be letters, digits, or an underscore. Names may not contain any other symbols, especially operators. Names are distinct up to 32 characters, but may be longer. Names may not be the same as any keyword. Names are case-sensitive.Examples
References to structure and union elements require compound names. The simple names in a compound name are joined with the dot operator (period).
cursor.loc.x = 10; // set structure element to 10Use the
#definedirective to create names for constants. These can be viewed as symbolic constants. See Section 4.5, "Macros."
#define READ 10
#define WRITE 20
#define ABS 0
#define REL 1
#define READ_ABS READ + ABS
#define READ_REL READ + RELThe term
READ_ABSis the same as 10 + 0 or 10, andREAD_RELis the same as 10 + 1 or 11. Note that Dynamic C does not allow anything to be assigned to a constant expression.READ_ABS = 27; // produces a compiler errorTo accomplish the above statement, do the following:
#undef READ_ABS
#define READ_ABS 274.5 Macros
Macros may be defined in Dynamic C by using
#define. A macro is a name replacement feature. Dynamic C has a text preprocessor that expands macros before the program text is compiled. The programmer assigns a name, up to 31 characters, to a fragment of text. Dynamic C then replaces the macro name with the text fragment wherever the name appears in the program. In this example,
#define OFFSET 12
#define SCALE 72
int i, x;
i = x * SCALE + OFFSET;the variable
igets the valuex * 72 + 12. Macros can have parameters such as in the following code.
#define word( a, b ) (a<<8 | b)
char c;
int i, j;
i = word( j, c ); // same as i = (j << 8 | c)The compiler removes the surrounding white space (comments, tabs and spaces) and collapses each sequence of white space in the macro definition into one space. It places a
\before any"or\to preserve their original meaning within the definition.4.5.1 Macro Operators # and ##
Dynamic C implements the
#and##macro operators.The
#operator forces the compiler to interpret the parameter immediately following it as a string literal. For example, if a macro is defined#define report(value,fmt)\
printf( #value "=" #fmt "\n", value )report( string, %s );printf( "string" "=" "%s" "\n", string );and because C always concatenates adjacent strings, the final result of expansion will be
printf( "string=%s\n", string );The
##operator concatenates the preceding character sequence with the following character sequence, deleting any white space in between. For example, given the macro#define set(x,y,z) x ## z ## _ ## y()set( AASC, FN, 6 );AASC6_FN();For parameters immediately adjacent to the
##operator, the corresponding argument is not expanded before substitution, but appears as it does in the macro call.4.5.2 Nested Macro Definitions
Generally speaking, Dynamic C expands macro calls recursively until they can expand no more. Another way of stating this is that macro definitions can be nested.
The exceptions to this rule are
Arguments to the
#and##operators are not expanded.To prevent infinite recursion, a macro does not expand within its own expansion.
The following complex example illustrates this.
#define A B
#define B C
#define uint unsigned int
#define M(x) M ## x
#define MM(x,y,z) x = y ## z
#define string something
#define write( value, fmt )\
printf( #value "=" #fmt "\n", value )The code
uint z;
M (M) (A,A,B);
write(string, %s);will expand first to
unsigned int z; // simple expansion
MM (A,A,B); // M(M) doesn't expand recursively
printf( "string" "=" "%s" "\n", string ); // #value"string" #fmt
"%s"
then to
unsigned int z;
A = AB; // from A = A ## B
printf( "string" "=" "%s" "\n", something );
// stringsomething
then to
unsigned int z;
B = AB; // AB
printf( "string=%s\n", something ); // concatenationand finally to
unsigned int z;
C = AB; // BC
printf("string = %s\n", something);4.5.3 Macro Restrictions
The number of arguments in a macro call must match the number of parameters in the macro definition. An empty parameter list is allowed, but the macro call must have an empty argument list. Macros are restricted to 32 parameters and 126 nested calls. A macro or parameter name must conform to the same requirements as any other C name. The C language does not perform macro replacement inside string literals, character constants, comments, or within a
#definedirective.A macro definition remains in effect unless removed by an
#undefdirective. If an attempt is made to redefine a macro without using#undef, a warning will appear and the original definition will remain in effect.4.6 Numbers
Numbers are constant values and are formed from digits, possibly a decimal point, and possibly the letters
U, L, X, orA-F, or their lower case equivalents. A decimal point or the presence of the letterEorFindicates that a number is real (has a floating-point representation).Integers have several forms of representation. The normal decimal form is the most common.
10 -327 1000 0An integer is long (32-bit) if its magnitude exceeds the 16-bit range (-32768 to +32767) or if it has the letter
Lappended.0L -32L 45000 32767LAn integer is unsigned if it has the letter
Uappended. It islongif it also hasLappended or if its magnitude exceeds the 16-bit range.0U 4294967294U 32767U 1700ULAn integer is hexadecimal if preceded by
0x.0x7E 0xE000 0xFFFFFFFAIt may contain digits and the letters
a-forA-F.An integer is octal if begins with zero and contains only the digits
0-7.0177 020000 000000630A real number can be expressed in a variety of ways.
4.5 means 4.54f means 4.00.3125 means 0.3125456e-31 means 456 × 10-310.3141592e1 means 3.1415924.7 Strings and Character Data
A string is a group of characters enclosed in double quotes ("").
"Press any key when ready..."Strings in C have a terminating null byte appended by the compiler. Although C does not have a string data type, it does have character arrays that serve the purpose. C does not have string operators, such as concatenate, but library functions
strcat()andstrncat()are available.Strings are multibyte objects, and as such they are always referenced by their starting address, and usually by a
char*variable. More precisely, arrays are always passed by address. Passing a pointer to a string is the same as passing the string. Refer to Section 4.15 for more information on pointers.The following code illustrates a typical use of strings.
const char * const select = "Select option\n";
char start[32];
strcpy(start,"Press any key when ready...\n");
printf( select ); // pass pointer to string
...
printf( start ); // pass stringNote that both the pointer and the elements of the array are explicitly defined as const. Some versions of Dynamic C allowed the second const to be omitted. Current versions of the compiler generate an error unless the second const is included.
4.7.1 String Concatenation
Two or more string literals are concatenated when placed next to each other. For example:
"Rabbits" "like carrots.""Rabbits like carrots."If the strings are on multiple lines, the macro continuation character must be used. For example:
"Rabbits"\
"don't like line dancing.""Rabbits don't like line dancing."4.7.2 Character Constants
Character constants have a slightly different meaning. They are not strings. A character constant is enclosed in single quotes (' ') and is a representation of an 8-bit integer value.
'a' '\n' '\x1B'Any character can be represented by an alternate form, whether in a character constant or in a string. Thus, nonprinting characters and characters that cannot be typed may be used.
A character can be written using its numeric value preceded by a backslash.
\x41 // the hex value 41
\101 // the octal value 101, a leading zero is optional
\B10000001 // the binary value 10000001There are also several "special" forms preceded by a backslash.
\a bell
\f formfeed
\r carriage return
\v vertical tab
\\ backslash
\' single quote\b backspace
\n newline
\t tab
\0 null character
\c the actual character c
\" double quoteExamples
4.8 Statements
Except for comments, everything in a C program is a statement. Almost all statements end with a semicolon. A C program is treated as a stream of characters where line boundaries are (generally) not meaningful. Any C statement may be written on as many lines as needed. Prior to Dynamic C 9.60, the compiler will parse up to 250 bytes for any single C statement in a ".c" or a ".lib" file. Starting with Dynamic C 9.60, the compiler will parse up to 64K bytes for any single C statement in a ".c" file; the 250 byte limit still exists for ".lib" files.
A statement can be many things. A declaration of variables is a statement. An assignment is a statement. A
whileorforloop is a statement. A compound statement is a group of statements enclosed in braces{and}. A group of statements may be single statements and/or compound statements.Comments (the
/*...*/kind) may occur almost anywhere, even in the middle of a statement, as long as they begin with/*and end with*/.4.9 Declarations
A variable must be declared before it can be used. That means the variable must have a name and a type, and perhaps its storage class could be specified. If an array is declared, its size must be given. Root data arrays are limited to a total of 32,767 elements.
If an aggregate type (
structorunion) is being declared, its internal structure has to be described as shown below.
struct { // description of structure
char flags;
struct { // a nested structure here
int x;
int y;
} loc;
} cursor;
...
int a;
a = cursor.loc.x; // use of structure element here4.10 Functions
The basic unit of a C application program is a function. Most functions accept parameters (a.k.a., arguments) and return results, but there are exceptions. All C functions have a return type that specifies what kind of result, if any, it returns. A function with a
voidreturn type returns no result. If a function is declared without specifying a return type, the compiler assumes that it is to return anint(integer) value.A function may call another function, including itself (a recursive call). The
mainfunction is called automatically after the program compiles or when the controller powers up. The beginning of themainfunction is the entry point to the entire program.4.11 Prototypes
A function may be declared with a prototype. This is so that:
Functions that have not been compiled may be called.
Recursive functions may be written.
The compiler may perform type-checking on the parameters to make sure that calls to the function receive arguments of the expected type.
A function prototype describes how to call the function and is nearly identical to the function's initial code.
/* This is a function prototype.*/
long tick_count ( char clock_id );/* This is the function's definition.*/
long tick_count ( char clock_id ){
...
}It is not necessary to provide parameter names in a prototype, but the parameter type is required, and all parameters must be included. (If the function accepts a variable number of arguments, as
printfdoes , use an ellipsis.)
/* This prototype is as good as the one above. */
long tick_count ( char );/* This is a prototype that uses ellipsis. */
int startup ( device id, ... );4.12 Type Definitions
Both types and variables may be defined. One virtue of high-level languages such as C and Pascal is that abstract data types can be defined. Once defined, the data types can be used as easily as simple data types like
int,char, andfloat. Consider this example.Use
typedefto create a meaningful name for a class of data. Consider this example.
typedef unsigned int node;
void NodeInit( node ); // type name is informative
void NodeInit( unsigned int ); // not very informativeThis example shows many of the basic C constructs.
The program above calculates the sum of squares of two numbers,
gandh, which are initialized to 10 and 12, respectively. The main function calls theinitfunction to give values to the global variablesgandh. Then it uses thesumSquarefunction to perform the calculation and assign the result of the calculation to the variablex. It prints the result using the library functionprintf, which includes a formatting string as the first argument.Notice that all functions have
{and}enclosing their contents, and all variables are declared before use. The functionsinit()andsumSquare()were defined before use, but there are alternatives to this.This was explained in Section 4.11.4.13 Aggregate Data Types
Simple data types can be grouped into more complex aggregate forms.
4.13.1 Array
A data type, whether it is simple or complex, can be replicated in an array. The declaration
int item[10]; // An array of 10 integers.represents a contiguous group of 10 integers. Array elements are referenced by their subscript.
j = item[n]; // The nth element of the array.Array subscripts count up from 0. Thus,
item[7]above is the eighth item in the array. Notice the[and]enclosing both array dimensions and array subscripts. Arrays can be "nested." The following doubly dimensioned array, or "array of arrays."
int matrix[7][3];is referenced in a similar way.
scale = matrix[i][j];The first dimension of an array does not have to be specified as long as an initialization list is specified.
int x[][2] = { {1, 2}, {3, 4}, {5, 6} };
char string[] = "abcdefg";4.13.2 Structure
Variables may be grouped together in structures (
structin C) or in arrays. Structures may be nested.
struct {
char flags;
struct {
int x;
int y;
} loc;
} cursor;Structure members--the variables within a structure--are referenced using the dot operator.
j = cursor.loc.xThe size of a structure is the sum of the sizes of its components.
4.13.3 Union
A union overlays simple or complex data. That is, all the union members have the same address. The size of the union is the size of the largest member.
union {
int ival;
long jval;
float xval;
} u;Unions can be nested. Union members--the variables within a union--are referenced, like structure elements, using the dot operator.
j = u.ival4.13.4 Composites
Composites of structures, arrays, unions, and primitive data may be formed. This example shows an array of structures that have arrays as structure elements.
typedef struct {
int *x;
int c[32]; // array in structure
} node;node list[12]; // array of structuresRefer to an element of array
c(above) as shown here.
z = list[n].c[m];
...
list[0].c[22] = 0xFF37;4.14 Storage Classes
Variable storage can be
autoorstatic. The term "static" means the data occupies a permanent fixed location for the life of the program. The term "auto" refers to variables that are placed on the system stack for the life of a function call.The default storage class isauto, but can be changed by using#class static. The default storage class can be superseded by the use of the keywordautoorstaticin a variable declaration.These terms apply to local variables, that is, variables defined within a function. If a variable does not belong to a function, it is called a global variable--available anywhere in the program--but there is no keyword in C to represent this fact. Global variables always have static storage.
The
registertype is reserved, but is not currently implemented. Dynamic C will change a variable to be of typeautoifregisteris encountered. Even though theregisterkeyword is not implemented, it still can not be used as a variable name or other symbol name. Its use will cause unhelpful error messages from the compiler.4.15 Pointers
A pointer is a variable that holds the 16-bit logical address of another variable, a structure, or a function. The indirection operator (*) is used to declare a variable as a pointer. The address operator (&) is used to set the pointer to the address of a variable.
int *ptr_to_i;
int i;
ptr_to_i = &i; // set pointer equal to the address of i
i = 10: // assign a value to i
j = *ptr_to_i; // this sets j equal to the value in iIn this example, the variable
ptr_to_iis a pointer to an integer. The statement "j = *ptr_to_i;" references the value of the integer by the use of the asterisk. Using correct pointer terminology, the statement dereferences the pointerptr_to_i. Then*ptr_to_iandihave identical values.Note that
ptr_to_iandido not have the same values becauseptr_to_iis a pointer andiis anint. Note also that*has two meanings (not counting its use as a multiplier in others contexts) in a variable declaration such asint *ptr_to_i;the*means that the variable will be a pointer type, and in an executable statementj = *ptr_to_i;means "the value stored at the address contained inptr_to_i."Pointers may point to other pointers.
It is possible to do pointer arithmetic, but this is slightly different from ordinary integer arithmetic. Here are some examples.
float f[10], *p, *q; // an array and some ptrs
p = &f; // point p to array element 0
q = p+5; // point q to array element 5
q++; // point q to array element 6
p = p + q; // illegal!Because the
floatis a 4-byte storage element, the statementq = p+5sets the actual value ofqtop+20.The statementq++adds 4 to the actual value ofq. Iffwere an array of 1-byte characters, the statementq++adds 1 toq.Beware of using uninitialized pointers. Uninitialized pointers can reference ANY location in memory. Storing data using an uninitialized pointer can overwrite code or cause a crash.
A common mistake is to declare and use a pointer to
char, thinking there is a string. But an uninitialized pointer is all there is.
char* string;
...
strcpy( string, "hello" ); // Invalid!
printf( string ); // Invalid!Pointer checking is a run-time option in Dynamic C. Use the Compiler tab on the Options | Project Options menu. Pointer checking will catch attempts to dereference a pointer to unallocated memory. However, if an uninitialized pointer happens to contain the address of a memory location that the compiler has already allocated, pointer checking will not catch this logic error. Because pointer checking is a run-time option, pointer checking adds instructions to code when pointer checking is used.
4.16 Pointers to Functions, Indirect Calls
Pointers to functions may be declared. When a function is called using a pointer to it, instead of directly, we call this an indirect call.
The syntax for declaring a pointer to a function is different than for ordinary pointers, and Dynamic C syntax for this is slightly different than the standard C syntax. Standard syntax for a pointer to a function is:
returntype (*name)( [argument list] );for example:
int (*func1)(int a, int b);
void (*func2)(char*);Dynamic C doesn't recognize the argument list in function pointer declarations. The correct Dynamic C syntax for the above examples would be:
int (*func1)();
void (*func2)();You can pass arguments to functions that are called indirectly by pointers, but the compiler will not check them for correctness. This means that the auto promotions provided by Dynamic C type checking will not occur, so values must be cast to the type that is expected or the size may not be correct. For example, if a function takes a long as a parameter, and you pass it a 16-bit integer value, it must be cast to type long in order for 4 bytes to be put onto the stack.
The following program shows some examples of using function pointers.
4.17 Argument Passing
In C, function arguments are generally passed by value. That is, arguments passed to a C function are generally copies on the program stack of the variables or expressions specified by the caller. Changes made to these copies do not affect the original values in the calling program.
In Dynamic C and most other C compilers, however, arrays are always passed by address. This policy includes strings (which are character arrays).
Dynamic C passes
structsby value on the stack. Passing a largestructtakes a long time and can easily cause a program to run out of memory. Pass pointers to largestructsif such problems occur.For a function to modify the original value of a parameter, pass the address of, or a pointer to, the parameter and then design the function to accept the address of the item.
4.18 Program Flow
Three terms describe the flow of execution of a C program: sequencing, branching and looping. Sequencing is simply the execution of one statement after another. Looping is the repetition of a group of statements. Branching is the choice of groups of statements. Program flow is altered by calling a function, that is transferring control to the function. Control is passed back to the calling function when the called function returns.
4.18.1 Loops
A
whileloop tests a condition at the start of the loop. As long as expression is true (non-zero), the loop body (some statement(s)) will execute. If expression is initially false (zero), the loop body will not execute. The curly braces are necessary if there is more than one statement in the loop body.
while( expression ){
some statement(s)
}A
doloop tests a condition at the end of the loop. As long as expression is true (non-zero) the loop body (some statement(s)) will execute. Adoloop executes at least once before its test. Unlike other controls, thedoloop requires a semicolon at the end.
do{
some statements
}while( expression );The
forloop is more complex: it sets an initial condition (exp1), evaluates a terminating condition (exp2), and provides a stepping expression (exp3) that is evaluated at the end of each iteration. Each of the three expressions is optional.
for( exp1 ; exp2 ; exp3 ){
some statement(s)
}If the end condition is initially false, a
forloop body will not execute at all. A typical use of theforloop is to countntimes.
sum = 0;
for( i = 0; i < n; i++ ){
sum = sum + array[i];
}This loop initially sets
ito 0, continues as long asiis less thann(stops wheniequalsn), and incrementsiat each pass.Another use for the
forloop is the infinite loop, which is useful in control systems.
for(;;){ some statement(s) }Here, there is no initial condition, no end condition, and no stepping expression. The loop body (some statement(s)) continues to execute endlessly. An endless loop can also be achieved with a
whileloop. This method is slightly less efficient than theforloop.
while(1) { some statement(s) }4.18.2 Continue and Break
Two keywords are available to help in the construction of loops:
continueandbreak.The
continuestatement causes the program control to skip unconditionally to the next pass of the loop. In the example below, ifbadis true, more statements will not execute; control will pass back to the top of thewhileloop.
get_char();while( ! EOF ){
some statements
if( bad ) continue;
more statements
}The
breakstatement causes the program control to jump unconditionally out of a loop. In the example below, ifcond_REDis true, more statements will not be executed and control will pass to the next statement after the ending curly brace of theforloop
for( i=0;i<n;i++ ){
some statements
if( cond_RED ) break;
more statements}The
breakkeyword also applies to theswitch/casestatement described in the next section. Thebreakstatement jumps out of the innermost control structure (loop or switch statement) only.There will be times when
breakis insufficient. The program will need to either jump out more than one level of nesting or there will be a choice of destinations when jumping out. Use agotostatement in such cases. For example,
while( some statements ){
for( i=0;i<n;i++ ){
some statements
if( cond_RED ) goto yyy;
some statements
if( code_BLUE ) goto zzz;
more statements
}
}
yyy:
handle cond_RED
zzz:
handle code_BLUE4.18.3 Branching
The
gotostatement is the simplest form of a branching statement. Coupled with a statement label, it simply transfers program control to the labeled statement.
some statements
abc:
other statements
goto abc;
...
more statements
goto def;
...
def:
more statementsThe colon at the end of the labels is required. In general, the use of the
gotostatement is discouraged in structured programming.The next simplest form of branching is the
ifstatement. The simple form of theifstatement tests a condition and executes a statement or compound statement if the condition expression is true (non-zero). The program will ignore theifbody when the condition is false (zero).
if( expression ){
some statement(s)
}A more complex form of the
ifstatement tests the condition and executes certain statements if the expression is true, and executes another group of statements when the expression is false.
if( expression ){
some statement(s) // if true
}else{
some statement(s) // if false
}The fullest form of the
ifstatements produces a succession of tests.
if( expr1 ){
some statements
}else if( expr2 ){
some statements
}else if( expr3 ){
some statements
...
}else{
some statements
}The program evaluates the first expression (expr1). If that proves false, it tries the second expression (expr2), and continues testing until it finds a true expression, an
elseclause, or the end of the if statement. Anelseclause is optional. Without anelseclause, anif/else ifstatement that finds no true condition will execute none of the controlled statements.The
switchstatement, the most complex branching statement, allows the programmer to phrase a "multiple choice" branch differently.
switch( expression ){caseconst1 :
statements1
break;caseconst2 :
statements2
break;caseconst3 :
statements3
break;
...
default:
statementsDEFAULT
}First the
switchexpression is evaluated. It must have an integer value. If one of theconstN values matches theswitchexpression, the sequence of statements identified by theconstN expression is executed. If there is no match, the sequence of statements identified by thedefaultlabel is executed. (Thedefaultpart is optional.) Unless thebreakkeyword is included at the end of the case's statements, the program will "fall through" and execute the statements for any number of other cases. Thebreakkeyword causes the program to exit theswitch/casestatement.The colons (
:) aftercaseanddefaultare required.4.19 Function Chaining
Function chaining allows special segments of code to be distributed in one or more functions. When a named function chain executes, all the segments belonging to that chain execute. Function chains allow the software to perform initialization, data recovery, and other kinds of tasks on request. There are two directives,
#makechainand#funcchain, and one keyword,segchainthat create and control function chains:#makechain chain_name
- Creates a function chain. When a program executes the named function chain, all of the functions or chain segments belonging to that chain execute. (No particular order of execution can be guaranteed.)
#funcchain chain_name name
- Adds a function, or another function chain, to a function chain.
segchain chain_name { statements }
- Defines a program segment (enclosed in curly braces) and attaches it to the named function chain.
Function chain segments defined with
segchainmust appear in a function directly after data declarations and before executable statements, as shown below.A program will call a function chain as it would an ordinary void function that has no parameters. The following example shows how to call a function chain that is named
recover.
#makechain recover
...
recover();4.20 Global Initialization
Various hardware devices in a system need to be initialized, not only by setting variables and control registers, but often by complex initialization procedures. Dynamic C provides a specific function chain,
_GLOBAL_INIT, for this purpose. Your program can add segments to the_GLOBAL_INITfunction chain, as shown in the example below.The special directive
#GLOBAL_INIT{ }tells the compiler to add the code in the block enclosed in braces to the_GLOBAL_INITfunction chain. Any number of#GLOBAL_INITsections may be used in your code. The order in which they are called is indeterminate since it depends on the order in which they were compiled. The storage class for variables used in a global initialization section must be static. Since the default storage class is auto, you must define variables as static in your application.The
_GLOBAL_INITfunction chain is always called when your program starts up, so there is nothing special to do to invoke it. In addition, it may be called explicitly at any time in an application program with the statement:_GLOBAL_INIT();Make this call this with caution. All costatements and cofunctions will be initialized. See Section 7.2 for more information about calling
_GLOBAL_INIT().4.21 Libraries
Dynamic C includes many libraries--files of useful functions in source code form. They are located in the \LIB directory where Dynamic C was installed. The default library file extension is
.LIB. Dynamic C uses functions and data from library files and compiles them with an application program that is then downloaded to a controller or saved to a.binfile.An application program (the default file extension is
.c) consists of a source code file that contains a main function (calledmain) and usually other user-defined functions. Any additional source files are considered to be libraries (though they may have a.cextension) and are treated as such. The minimum application program is one source file, containing only:main(){}Libraries (those defined by you and those defined by Rabbit) are "linked" with the application through the
#usedirective. The#usedirective identifies a file from which functions and data may be extracted. Files identified by#usedirectives are nestable, as shown below. (The#usedirective is a replacement for the#includedirective, which is not supported in Dynamic C.)
Most libraries needed by Dynamic C programs have #use statements in
lib\..\default.h.Section 4.23 explains how Dynamic C knows which functions and global variables in a library are available for use.
4.21.1 LIB.DIR
Any library that is to be #use'd in a Dynamic C program must be listed in the file
LIB.DIR, or another*.DIRfile specified by the user.The lib.dir strategy starting with Dynamic C 9.30 allows naming a folder with optional mask(s). No mask implies *.* and multiple masks are separated by ";" so that "lib" and "lib\*.*" both include all files and "
lib\*.lib;*.c;*.h*" includes all files with extensions of.lib,.cand.h. Dynamic C generated file (e.g.,.mdl,.hxl, etc.) are not parsed, which means they are excluded when using the wildcard mask.Dynamic C now enforces unique file extension names regardless of path, so that "#use myfile.lib" can not use an unintended copy of
myfile.libas the list of pathnames included inlib.diris searched for the first occurrence of that file extension. An error message naming both full paths will come up when trying to compile ANY program alerting the user of the infraction.4.22 Headers
The following table describes two kinds of headers used in Dynamic C libraries.
You may also notice some "Library Description" headers at the top of library files. These have no special meaning to Dynamic C, they are simply comment blocks.
4.23 Modules
A Dynamic C library typically contains several modules. Modules must be understood to write efficient custom libraries. Modules provide Dynamic C with the names of functions and variables within a library that may be referenced by files that have a
#usedirective for the library somewhere in the code.Modules organize the library contents in such a way as to allow for smaller code size in the compiled application that uses the library. To create your own libraries, write modules following the guidelines in this section.
The scope of modules is global, but indeterminate compilation order makes the situation less than straightforward. Read this entire section carefully to understand module scope.
4.23.1 The Parts of a Module
A module has three parts: the key, the header, and the body. The structure of a module is:
/*** BeginHeader func1, var2, .... */
prototype for func1
extern var2
/*** EndHeader */
definition of func1
declaration for var2
possibly other functions and dataA module begins with its
BeginHeadercomment and continues until either the nextBeginHeadercomment or the end of the file is encountered.4.23.1.1 Module Key
The module key is usually contained within the first line of the module header. It is a list of function and data names separated by commas. The list of names may continue on subsequent lines.
/*** BeginHeader [name1, name2, ....] */It is important to format the
BeginHeadercomment correctly, otherwise Dynamic C cannot find the contents of the module. The case of the word "beginheader" is unimportant, but it must be preceded by a forward slash, 3 asterisks and one space (/*** ). The forward slash must be the first character on the line. TheBeginHeadercomment must end with an asterisk and a forward slash (*/).The key tells the compiler which functions exist in the module so the compiler can exclude the module if names in the key are not referenced. Data declarations (constants, structures, unions and variables) as well as macros and function chains (both
#makechainand#funchainstatements) do not need to be named in the key if they are completely defined in the header, i.e, noexterndeclaration. They are fully known to the compiler by being completely defined in the module header. An important thing to remember is that variables declared in a header section will be allocated memory space unless the declaration is preceded withextern.4.23.1.2 Module Header
Every line between the
BeginHeaderandEndHeadercomments belongs to the header of the module. When a library is linked to an application (i.e., the application has the statement: #use "library_name"), Dynamic C precompiles every header in the library, and only the headers.With proper function prototypes and variable declarations, a module header ensures proper type checking throughout the application program. Prototypes, variables, structures, typedefs and macros declared in a header section will always be parsed by the compiler if the library is #used, and everything will have global scope. It is even permissible to put function bodies in header sections, but it's not recommended because the function will be compiled with any application that #uses the library. Since variables declared in a header section will be allocated memory space unless the declaration is preceded with
extern, the variable declaration should be in the module body instead of the header to save data space.The scope of anything inside the module header is global; this includes compiler directives. Since the headers are compiled before the module bodies, the last one of a given type of directive encountered will be in effect and any previous ones will be forgotten.
Using compiler directives like
#classor#memmapinside module headers is inadvisable. If it is important to set, for example, "#class auto" for some library modules and "#class static" for others, the appropriate directives should be placed inside the module body, not in the module header. Furthermore, since there is no guaranteed compilation order and compiler directives have global scope, when you issue a compiler directive to change default behavior for a particular module, at the end of the module you should issue another compiler directive to change back to the default behavior. For example, if a module body needs to have its storage class as static, have a "#class static" directive at the beginning of the module body and "#class auto" at the end.4.23.1.3 Module Body
Every line of code after the
EndHeadercomment belongs to the body of the module until (1) end-of-file or (2) theBeginHeadercomment of another module. Dynamic C compiles the entire body of a module if any of the names in the key or header are referenced anywhere in the application. So keep modules small, don't put all the functions in a library into one module. If you look at the Dynamic C libraries you'll notice that many modules consist of one function. This saves on code size, because only the functions that are called are actually compiled into the application.To further minimize waste, define code and data only in the body of a module. It is recommended that a module header contain only prototypes and
externdeclarations because they do not generate any code by themselves. That way, the compiler will generate code or allocate data only if the module is used by the application program.4.23.2 Module Sample Code
There are many examples of modules in the
Libdirectory of Dynamic C. The following code will illustrate proper module syntax and show the scope of directives, functions and variables.
There are three modules defined in this code. The first one is responsible for the variable
ticks, the second and third modules define functionsGet_Ticks()andInc_Ticksthat access the variable. AlthoughInc_Ticksis an assembly language routine, it has a function prototype in the module header, allowing the compiler to check calls to it.If the application program calls
Inc_TicksorGet_Ticks()(or both), the module bodies corresponding to the called routines will be compiled. The compilation of these routines triggers compilation of the module body corresponding toticksbecause the functions use the variableticks.
Let's say the above file is named
mylibrary.lib. If an application has the statement#use "mylibrary.lib"and then callsfunc_b(), will the printf statement be reached? The answer is no. The order of compilation for module headers is sequential from the beginning of the file, therefore, the macroSECONDHEADERis undefined when the first module header is parsed.If an application #uses this library and then makes a call to
func_a(), will that function's print statement be reached? The answer is yes. Since all the headers were compiled first, the macroSECONDHEADERis defined when the first module body is compiled.4.23.3 Important Notes
Remember that in a Dynamic C application there is only one file that contains
main(). All other source files used by the file that containsmain()are regarded as library files. Each library must be included in aLIB.DIR(or a user defined replacement for it). Although Dynamic C uses.LIBas the library extension, you may use anything you like as long as the complete path is entered in yourLIB.DIRfile.There is no way to define file scope variables in Dynamic C libraries.
4.24 Function Description Headers
Each user-callable function in a Dynamic C library has a descriptive header preceding the function to describe the function. Function headers are extracted by Dynamic C to provide on-line help messages.
The header is a specially formatted comment, such as the following example.
If this format is followed, user-created library functions will show up in the Function Lookup <Ctrl+H> feature if the library is listed in
lib.diror its replacement. Note that these sections are scanned in only when Dynamic C starts.4.25 Support Files
Dynamic C has several support files that are necessary in building an application. These files are listed below.
Table 4-5. Dynamic C Support Files DCW.CFGContains configuration data for the target controller. DC.HHContains prototypes, basic type definitions, #define, and default modes for Dynamic C. This file can be modified by the programmer.DEFAULT.HContains a set of #usedirectives for each control product that Rabbit ships. This file can be modified.LIB.DIRContains pathnames for all libraries that will be known to Dynamic C. The programmer can add or remove libraries from this list. The factory default is for this file to contain all the libraries on the Dynamic C distribution disk. Any library that is to be used in a Dynamic C program must be listed in the file LIB.DIR, or another*.DIRfile specified by the user.PROJECT.DCP
DEFAULT.DCPThese files hold the default compilation environment that is shipped from the factory.
DEFAULT.DCPmay be modified, but notPROJECT.DCP. See Chapter 16 for details on project files.