Wednesday, January 23, 2008

Stomach Pain Dark Stool

The programming language C #. The preprocessor.

Author: José Antonio González Seco
 full course available in DOC and PDF formats the author's web 

.
  Preprocessing  
 
preprocessor Concept

  • preprocessing directives
  • preprocessor Concept
  • The preprocessed is a prerequisite the compilation by which you can control how it is made. The preprocessor auxiliary module is used by the compiler to perform these tasks, and finally the compiler compiles the result of applying the text file preprocessor source, a result that is also a text file. Note then, that while the compiler does a translation of text to binary, so the preprocessor does is a translation from text to text. Those who have experience in the use of preprocessor languages \u200b\u200blike C + + and know the problems involved in land use can breathe easy, because in C # you have eliminated most of the features of it that caused errors difficult to detect (macros , include directives, etc..) practically only used to allow for conditional compilation of code.
Continue Reading ...


Policy Concept. Syntax The preprocessor does not interpret in any way the source code file, but only interprets the file what are called preprocessor directives
character) For example: # define TEST There was a fatal error not worry now if understand the meaning of these directives, as will be explained later. All must know is that the name shown after the # symbol is the name of the policy, and the text that comes after him (not all policies necessarily have to include it) is the value that is given. Therefore, the syntax of a directive is: #
\u0026lt;nombreDirectiva>
\u0026lt;valorDirectiva>
may include comments on the same line when declaring a policy, but these can only be a line comments starting with
    / /
  • . For example, the comment is valid: # define TEST / / Has there been any errors during the preprocessing But this one does not, because although , a line has the syntax of the comments may span multiple lines:
      As already mentioned, the main usefulness of the preprocessor in C # is possible to determine which regions of code in a source file be compiled. To do this, what is done is to enclose optional sections of code within conditional compilation directives, so that only compiled if certain preprocessor identifiers are defined. To define a tag of this type of policy used follow this syntax: # define
    • \u0026lt;nombreIdentificador>
    • This directive defines a preprocessor identifier \u0026lt;nombreIdentificador>. Although further study in detail what are the valid names as identifiers in C #, for now we consider to be valid those formed by one or more alphanumeric characters such
    that are neither true nor

false and do not start with a number. For example, to define a preprocessor identifier would name TEST: TEST # define By convention, these identifiers are given names in all uppercase letters, as in the example above. Although it is only a convention and no requirement to use it, this is the nomenclature we will use in this document and is the one that follows Microsoft in its example code. Become familiar with it because there are a lot of code written that uses it and why use it easier for others to read your code as it is the notation we expect to find. is important to note that any definition of identifier must precede any use of code in the source file. For example, the following code is invalid because it before # define have included source code (the class A):
class A {} # define TEST
However, although there can be no code before a # define themselves that there is freedom to precede other preprocessor directives. there an alternative way to define a preprocessor identifier and also allows the definition only applies in a particular compilation. This form is to pass to the compiler in its call option
/ d: \u0026lt;nombreIdentificador>
(short for

/ define: \u0026lt;nombreIdentificador> ), in which case during compilation are considered to be the beginning of all source files compile the identifier is defined above. The following three types of call are equivalent and define compiler preprocessor identifiers name TEST and TRACE when compiling a source file name ejemplo.cs csc / d: TEST / d: TRACE ejemplo.cs csc / d: TEST , TRACE ejemplo.cs csc / d: TEST; TRACE ejemplo.cs Note in the example that if we want to define more than one identifier using this technique we have two alternatives: to include several options / d the call to the compiler or define various these identifiers in the same option
/ d
characters separated by comma (, ) or semicolon ( ; ) When working with Visual Studio.NET, rather than directly to the command line compiler, then you can get the same effect through
View
à
Property Pages
à Configuration Options

 
  à    Build 
à
 
 Conditional Compilation Constants 
, where he again used the semicolon (; ) or comma (, ) as separators, you can define multiple constants . For everything to work well, before selecting View
has
selected in the Solution Explorer
  (opens with  View 
à
 
Solution Explorer) project to apply the definition of the constants.

Finally, regarding the use of # define
 
only comment is that you can define the same policy several times without causing any error in the compiler, allowing us to pass as many values \u200b\u200bof the option / d compiler as you want without fear of conflict with IDs already included preprocessing the source to compile. Elimination

identifiers preprocessing
 the same way that you can define preprocessor identifiers, you can also delete the definitions of this type of identifiers previously made. To this end the policy that is used has the following syntax: # undef 
\u0026lt;nombreIdentificador> If you try to eliminate this policy an identifier that has not been defined or the definition of which has already been eliminated there will be no error, but simply the removal policy will be ignored. The following example shows an example of this in the second # undef is ignored: # define Version1 Version1 # undef # undef Version1 As was the case with # define directives

, you can not include source code before # undef directives but, at most, all that could be included before they would preprocessor directives. Conditional Compilation As has been repeated several times over the issue, the main use of preprocessor in C # is to allow the compilation of conditional code, which is only allowed to compile certain regions source if the defined preprocessor variables satisfy some given condition. To achieve this using the following set of directives: # if \u0026lt;condición1> \u0026lt;código1> # elif \u0026lt;condición2> \u0026lt;código2> ... # Else # endif \u0026lt;códigoElse> The meaning of a structure like this is that if you meet \u0026lt;condición1>> is then passed to the compiler \u0026lt;código1>, if this happens but meets \u0026lt;condición2> , then what would the compiler would \u0026lt;código2>, and so continuously until to reach a branch # elif whose condition is met. Failure to meet any but one branch # else will be passed to the compiler \u0026lt;códigoElse>, but if this industry did not exist then it would pass any code and continue preprocessing the following code to # endif in original source. Although the branches # else # eldif and are optional, we must be careful not to mix, as the branch # else can only appear as the last branch block # if ... # Endif . various structures can be nested # If ... # Endif , as the following code:

# define TEST using System; class A {public static void Main () {# if TEST Console.Write ("This is a test") # if TRACE Console. Write ("to draw") # elif! TRACE Console.Write ("no trace"), # endif # endif}} As shown in the example, the conditions are specified preprocessing identifier names, considering that each condition only true if the identifier that follows it is defined. Or what is the same: a better way preprocessing identifier ( true in C #) if defined and false (false in C #) or not.

symbol! included in next to the policy value # elif is the symbol for "no" logical and

# elif

  used in allowing us to indicate that if that is not defined TRACE ID preprocessed have to pass to the compiler instructions listed below (ie, the Console.Write ("no trace");  
The source code will the compiler preprocessor if you compile without specifying any / d the call to the compiler is:
 using System; 
class A {static void Main () {Console.Write ("This is a test") Console.Write ("no trace"); }} Note how the code passed to the compiler and do not see any preprocessor directives, the preprocessor because what happens is the code resulting from applying the original preprocessor directives it contained. Also, if compilásemos original source code by calling the compiler with / d: TRACE, what would the compiler preprocessor is: using System;
class A {public static void Main () {Console.Write ("This is a test") Console.Write ("no trace");}}
Until now we have seen that the condition of a # if or # elif identifier may be preprocessed, and worth it true or false depending on whether or not defined. Well, these are not the only type of valid conditions in C #, but it is also possible to include terms containing logical expressions formed by pre-processing identifiers, logical operators (
!
for "not",
 & &  for "and" and   TEST TRACE as are / / defined or if no one is. # If TRACE! = TEST / / only be met if TRACE is defined and TEST or not / / vice versa # if TRACE & & TEST / / only be met if defined TRACE and TEST. # If TRACE  special meaning they have in the conditions of  # if  and  # elif  
Generation of warnings and errors

Preprocessor C # also provides guidelines for generating warnings and errors during the process preprocessing should become interpreted by the preprocessor. These directives have the following syntax: # warning # error \u0026lt;mensajeAviso> \u0026lt;mensajeError> The directive # warning making process is caused when the compiler produces a warning message that follows the format standard used by him for that and descriptive text of which has the contents listed on \u0026lt;mensajeAviso>, and # error
does the same thing causing an error message instead of a warning.
Using conditional compilation directives can control when they have to produce these messages, when they have to process these directives. In fact the main use of these guidelines is to allow error control assigning values \u200b\u200bto different preprocessing identifier code and an example of this is: # warning not yet revised Code # define TEST # if TEST & & FINAL # error A code can not be simultaneously test and final version # endif class A {} This code will always produce the warning message, but the # if
 indicates that only produce error message if you have defined IDs simultaneously preprocessed TEST and FINAL. 

As can be seen from the example, the preprocessor of C # considers that messages related to directives
# warning or # error
are all text that is after the name of such directives and to the end of the line which they appear. Therefore, any comments to be included in a line of this type will be considered as part of message to be displayed, not as a comment as such. For example, before the board:
# Compilation failed error / / Error What will display a message as follows: Fichero.cs (3.5): error CS1029: The compilation has failed / / Error Changes line numbering
By default, the compiler lists each source file lines as the normal order in which they appear in it, and this order is the following information when errors or warnings during compilation. However, there are situations that are interested in changing these numbers, and it provides a policy with the following syntax: # line
\u0026lt;number>
 
"


\u0026lt;filename>
" 
This directive tells the preprocessor to be considered by the next line of the source file that appears is the line whose number is indicated, regardless of the value that the numbering had used at that time. The value shown on " \u0026lt;filename> " is optional and should appear indicating the name to be considered to have the file at a time to give error messages. A eg # line 127 "csmace.cs" This use of # line that the compiler has to consider the next line is line 127 of file csmace.cs. Since she will continue using the normal numbering system (the one after that will be the 128, csmace.cs, the next the 129, etc.) Unless later renumber again with another # line directive . Although in principle it may seem that this policy is of little use, the fact is that usually comes fairly well for writing compilers and other tools that generate code in C # from code written in other languages. Marked source regions
 
may mark regions of code and associate a name using the guidelines set # region # endregion and . These directives are used as follows: # region # endregion \u0026lt;nombreRegión> \u0026lt;code> income given to these markings depends on the tool, but at the time of writing the only tool that made use of them Visual Studio.NET was where it is used to mark code so that from the code window can expand and contract with a single mouse click. Specifically, the code window of Visual Studio you will see a symbol [-] next to the regions marked by code so that clicking on it all the code in the region will be compressed and replaced by the name given to \u0026lt; nombreRegión>. After this, the [-] will become a [+] and if we press the collapsed code will expand and regain its original appearance. Below is an example of each:

Figure 4: Region Code expanded

     
Figure 5: Region Code contracted

 Be careful when nesting areas with conditional compilation directives, since all block 
# if ... # Endif

beginning within a region has also finished inside. Therefore, the next use of the # region directive is not valid because it ends up being the block RegiónErrónea
 # if ... # endif open: 
# region RegiónErrónea # if A # endregion # endif
 
(C) 2001 José Antonio González Seco

  addthis_url = ''; addthis_title = ''; addthis_pub = 'igner';        

0 comments:

Post a Comment