14. C Language - The Pre Processor

Please download to get full document.

View again

of 5
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Published
C Language - The Preprocessor C Language - The Preprocessor In this tutorial you will learn about C Language - The Preprocessor, Preprocessor directives, Macros, #define identifier string, Simple macro substitution, Macros as arguments, Nesting of macros, Undefining a macro and File inclusion. C Language - The Preprocessor In this tutorial you will learn about C Language - The Preprocessor, Preprocessor directives, Macros, #define identifier string, Simple macro substitution, Macros as argument
  C Language - The Preprocessor C Language - The Preprocessor In this tutorial you will learn about C Language - The Preprocessor, Preprocessor directives, Macros, #define identifier string, Simple macro substitution, Macros asarguments, Nesting of macros, Undefining a macro and File inclusion. C Language- The Preprocessor In this tutorial you will learn about C Language - The Preprocessor, Preprocessor directives, Macros, #define identifier  string, Simple macro substitution, Macros as arguments, Nesting of macros, Undefining a macro and File inclusion. The Preprocessor A unique feature of c language is the preprocessor. A programcan use the tools provided by preprocessor to make his program easy to read, modify, portable and more efficient.Preprocessor is a program that processes the code before it passes through the compiler. Itoperates under the control of preprocessor command lines and directives. Preprocessor directives are placed in the source program before the main line before thesource code  passes through the compiler it is examined by the preprocessor for any preprocessor directives. If there is any appropriate actions are taken then the source program is handedover to the compiler.Preprocessor directives follow the special syntax rules and begin with the symbol #bincolumn1 and do not require any semicolon at the end. A set of commonly used preprocessor directives Preprocessor directives: Directive   Function  #defineDefines a macro substitution#undefUndefines a macro#includeSpecifies a file to be included#ifdefTests for macro definition#endifSpecifies the end of #if #ifndefTests whether the macro is not def #ifTests a compile time condition#elseSpecifies alternatives when # if test fails The preprocessor directives can be divided into three categories1. Macro substitution division2. File inclusion division3. Compiler control division Macros: Macro substitution is a process where an identifier in a program is replaced by a predefinedstringcomposed of one or more tokens we can use the #define statement for thetask.It has the following form #define identifier string The preprocessor replaces every occurrence of the identifier int the source code by astring. The definition should start with the keyword #define and should follow onidentifier and a string with at least one blank space between them. The string may be anytext and identifier must be a valid c name.There are different forms of macro substitution. The most common form is1. Simple macro substitution  2. Argument macro substitution3. Nested macro substitution Simple macro substitution: Simple string replacement is commonly used to define constants example: #define pi 3.1415926 Writing macro definition in capitals is a convention not a rule a macro definition caninclude more than a simple constant value it can include expressions as well. Followingare valid examples: #define AREA 12.36 Macros as arguments: The preprocessor permits us to define more complex and more useful form of replacements it takes the following form. # define identifier(f1,f2,f3…..fn) string.  Notice that there is no space between identifier and left parentheses and the identifier f1,f2,f3 …. Fn is analogous to formal arguments in a function definition.There is a basic difference between simple replacement discussed above and replacementof macro arguments is known as a macro call  A simple example of a macro with arguments is # define CUBE (x) (x*x*x) If the following statements appears later in the program, volume=CUBE(side); The preprocessor would expand the statement to volume =(side*side*side) Nesting of macros: We can also use one macro in the definition of another macro. That is macro definitionsmay be nested. Consider the following macro definitions # define SQUARE(x)((x)*(x)) Undefining a macro: A defined macro can be undefined using the statement # undef identifier.
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks