• Macros

    What are macros?

    • they tell the compiler to substitute an identifier with expressions, statements, or group of statements
    • you can think of macros as if they are a way to tell the compiler to replace some code with other code

    Why are macros important?

    • they make your code easier to read and maintain

    macro.c:

    common_constants.h:

    Things to note:

  • More Advanced Macros and Compilation Techniques

    Macros for expressions and multiline statements

    • they can be faster than using functions
    • functions are more flexible and easier to maintain
    • recommend using functions first and only switch to macros if there's a need for it
    • examples:
      • Macro for an expression:

        area.c:

      • Macro for a function:

        count.c:

    Conditional Compilation

    • compiling some code based on a condition
    • useful for inserting debug statements when necessary
    • example:
      • Compile a debug statement only when debugging:

        debug.c:

  • Update net_balance.c to have Conditionally Compiled Debug Statements

    Update net_balance.c with conditional debug statements:

      print out the following to help debug net_balance.c:
    • account_balance
    • account_type
  • Assignment

    Homework Problem 17.1, at the end of this problem you should submit to Laulima:

    • a file named UHusername_17_1.c
    • a file named UHusername_17_1_macros.h
      • example:
      • zhaol_17_1.c
      • zhaol_17_1_marcros.h
    • write a program that includes a macro file UHusername_17_1_macros.h and will be similar to common_constants.h)

      • the macro file will define the following:
        • a macro named PI to 2 decimal places
        • a macro expression named CIRCUMFERENCE to calculate the circumference of the circle
        • a macro expression named CIRCLE_AREA to calculate the area of a circle
        • a macro expression named SQUARE_AREA to calculate the area of a square
      • the program will ask for a length and then calculate the following:
        • circumference of a circle
        • area of a circle
        • area of a square
      • Test Case 1:
        Please enter a length:
        [user enters 3]
        the circumference of the circle is: 18.84
        the area of the circle is: 28.26
        the area of the square is: 9.00