programare i (curs 7)

Upload: sandu405

Post on 02-Jun-2018

242 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Programare I (Curs 7)

    1/14

    1

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    Limbajul de programare C

  • 8/10/2019 Programare I (Curs 7)

    2/14

    2

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    The C Preprocessor

    A pre-compiling step which performs some textual substitutions on a Csource text, based on a set of directives.

    C source textwithpre-processor directives pure C source text

    (no pre-processor directives)

    preprocessing

    Usually, the preprocessor is integrated with the compiler in a single executable file.

    Some implementations supply also a standalone preprocessor, located in the bin folder of the installation

    *The names of preprocessor directives are notreserved keywords of the language!

  • 8/10/2019 Programare I (Curs 7)

    3/14

    3

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    The C Preprocessor: types of actions

    Source file inclusion:

    Macro replacement:

    Conditional inclusion:

    Line control :

    Error directive:

    Pragma directive:

    #include

    #define, #undef

    #if, #ifdef, #ifndef, #else, #elif, #endif

    #line

    #error

    #pragma

  • 8/10/2019 Programare I (Curs 7)

    4/14

    4

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    C Preprocessor directives: #include

    Usage:source file inclusionSyntax:

    #include

    #include file_name

    The difference between the two syntax forms:

    - in the first form, the search of the file to be included starts in an installation defined path

    - in the second form, the search starts in the folder where the source resides

    Action:

    Note:Although any text file may be included, the files which are included that way should be header files!

    the lines of the cited file are included in the current file, starting with the line following the

    include-directive

  • 8/10/2019 Programare I (Curs 7)

    5/14

    5

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    Header files should not contain code, just:- preprocessor directives

    - typedefs

    - external declarations

    - function prototypes

    Header files should not be repeatedely

    included.

    There is a standard way of preventing this:

    C Preprocessor directives: #includeavoiding repetead inclusions

    E.g.

    #define FALSE 0

    #define TRUE 1

    typedef long Coord;

    extern Coord x0, y0;

    extern globalCount;Coord getX();

    Coord getY();

    #ifndef H_MODULE#define H_MODULE

    /*content to be included*/

    #endif

    Module.h

  • 8/10/2019 Programare I (Curs 7)

    6/14

    6

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    C preprocessor directives: #define

    Usage: Associates a nameto a substitution string.

    The name is usually called: a macro nameor simply macro.

    Syntax:

    #define macro_name substitution string

    #define macro_name(parameters) substitution string

    ActionThe name (and parametersif they exist) and the substituion string are placed in a symbol table.

    During preprocessing of subsequent source lines, untill a #undef macro_namedirective or untill

    the end of the source file, each occurrence of the macro_nameis replaced by the substitution

    string. This is also called macro substitutionor macro expansion.

    Ex.

    #define PI 3.14159#define TRUE 1

    #define FALSE 0

    #define GET_HIGH_BYTE_MASK 0xFF00

    #define CLEAR_HIGH_BYTE_MASK 0x00FF

    Note:

    Traditionally, macro namesare all-upper-case-letters

  • 8/10/2019 Programare I (Curs 7)

    7/147

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    C preprocessor directives: #undef

    Usage:un-defines a macro name

    Syntax:

    #undef macro_name

    Action:Eliminates the name macro_name from the symbol table managed by the

    preprocessor. Any subsequent reference to the name will result in an error message

    of the type: "Undefined symbol".

  • 8/10/2019 Programare I (Curs 7)

    8/148

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    The C Preprocessor: parameterized macros

    Eg.- definitions:

    #define max(A, B) ((A) > (B) ? (A) : (B))

    #define square(x) (x) * (x)

    - calls:

    Sintactically, a macro call is similar to a function call!During preprocessing, the macro-call is replaced by the substitution string in

    which the formal parametersare replaced with the actual arguments

    (macro-expansion)

    (x1)>(x2) ? (x1) : (x2)(z)>(1000) ? (z) : (1000)

    (r)>(-1.75) ? (r) : (-1.75)

    (a)*(a)

    (1.5)*(1.5)

    max(x1, x2)max(z, 1000)

    max(r, -1.75)

    square(a)

    square(1.5)

    macro-

    expansion

  • 8/10/2019 Programare I (Curs 7)

    9/149

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    Why macros? Macros vs functions

    Using macros results in faster code because no call overhead is incurred!

    No type checking is performed, a macro may be called with arguments of

    different types

    Because macros are expanded in-line, they may change the actual

    arguments (see swapexample!

    They are a means of making code more lizible

    On the other hand :

    functions are safer, because of the type checking performed on the actual

    arguments

    using functions reduces the size of the executable code

  • 8/10/2019 Programare I (Curs 7)

    10/1410

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    The C Preprocessor: parameterized macros - pitfalls

    #define max(A, B) A > B ? A : B

    #define square(x) x * x

    result=square(n);

    result=square(n+1);

    result=n*n

    result=n+1*n+1

    macro expansion

    macro expansion

    #define swap(a, b) { a=a^b; b=a^b; a=a^b;}

    #define swap(a, b) {int temp; temp=a; a=b; b=temp;}

    #define swap(a, b, type) {type temp; \

    temp=a; \

    a=b; \

    b=temp; \

    }

  • 8/10/2019 Programare I (Curs 7)

    11/1411

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    C Preprocessor directives: conditional compilation

    #idef macro_name...

    #endif

    #ifndef macro_name...#endif

    #if constant_expression...#endif

    #if !constant_expression...#endif

    #if defined macro_name...#endif

    #if !defined macro_name...#endif

  • 8/10/2019 Programare I (Curs 7)

    12/1412

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    C Preprocessor directives: #error

    Usage:diagnostic messages during preprocessing

    Syntax:

    #error text

    Action: produces a diagnostic message that includes the specified text

  • 8/10/2019 Programare I (Curs 7)

    13/1413

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    C Preprocessor directives: #pragma

    Usage:implementation defined actions

    Syntax:

    #pragma implementation_specific_directive

    Action:Announces an implementation specific action (e.g. grouping ofvariables in special memory sections)

  • 8/10/2019 Programare I (Curs 7)

    14/1414

    West University of TimisoaraFaculty of Mathematics and Informatics Programming I

    Lucian Cucu - The C Programming Language08.01.2015

    The Cpreprocessor: Predefined ANSI-C Macros

    __FILE__ is replaced by the name without extension of the current file.

    __LINE__ is replaced by the current line number.

    __TIME__ is replaced by a string containing the time when the compilationwas started.

    __DATE__ is replaced by a string containing the date when the compilationwas started.

    __STDC__ is set to 1 for all compilers that are built up according to the ANSI

    standard.