Preprocessing and Macros C Preprocessor Preprocessor

Preprocessing and Macros C Preprocessor Preprocessor

C Preprocessor • Invoked automatically by the C compiler C program A 1st pass: invokes C preprocessor A 2nd pass: invokes compiler on the resulting C code Preprocessing and Macros Preprocessor • Manually invoke C preprocessor gcc –E foo.c Modified C program CS 217 Compiler Object code 1 2 Preprocessor Directives File Inclusion • Three kinds of directives • Why? A File inclusion A Allow a program or a module’s implementation to use certain – #include interfaces A Conditional compilation – #if, #ifdef, #ifndef, #elif, #else, #endif • An interface or a header file contains declarations for a module A Macros – #define A Name of the header file should end in .h ... • Rules • User-define header files “ ” #include “mydefs.h ” A Always starts with a line with “#” A Can appear anywhere in a program • System header files: < ... > A Comments may appear on the same line #include <stdio.h> A Takes one line unless explicitly continue – #define MAX_CHARS 300 /* max file name size */ – #define MAX_FILES \ 100 3 4 Conditional Compilation Another Example • Why? Allows this file • Conditionally compile debugging code To be included A One source for many platforms or many cases A Need to have special source for specific situations Multiple times ... • Conditional compilation if (some expr) { #ifdef name #ifndef FOO_H some code #define FOO_H #ifndef name #ifdef DEBUG #if expr #ifdef WINDOWS_OS printf(“this path taken\n” ); #elif expr #include <windows.h> #endif #else #elif LINUX_OS } #include <linux.h> else #endif #endif . some other code; • Removing macro definitions . #undef plusone . #endif Allows selective gcc -DDEBUG foo.c (OS-specific) gcc -DWINDOWS_OS foo.c compilation 5 6 Macros Macros: Use “(“ and “)” • Provide parameterized text substitution • Always parenthesize macro parameters in definition • Why? #define plusone(x) x+1 A The code may be slightly faster A No type checking i = 3*plusone(2); • Macro definition becomes #define MAXLINE 120 i = 3*2+1 #define lower(c) ((c)-`A ’+‘a’) • Macro replacement #define plusone(x) ((x)+1) char buf[MAXLINE+1]; becomes i = 3*plusone(2); char buf[120+1]; becomes i = 3*((2)+1) c = lower(buf[i]); becomes c = ((buf[i])-`A ’+‘a’); 7 8 Macros: Careful about Side-Effects More on Macros: # Operator • “++” and “—” operators create side effects • # in the macro converts an argument into a string literal • Always avoid side-effects in parameters passed to macros #define PRINT_INT(x) printf( #x “= %d\n”, x) #define max(a, b) ((a)>(b)?(a):(b)) ... PRINT_INT( x * y ); y = max(i++, j++) ... becomes y = ((i++)>(j++)?(i++):(j++)); will become • Question ... A What data type can we use in the macro “max ” printf( “x * y” “= %d\n”, x*y); ... • Question A We now have “foo””bar” in printf, what does this mean? 9 10 More on Macros: ## Operator More on Macros: #error • You may never need to use this • Let the preprocessor print out an error message #error message • ## pastes two tokens into one • Example • Example #if defined(WINDOWS) #define GENERIC_MAX(type) \ ... type type##_max(type x, type y) \ #elif defined(LINUX) { return x > y ? x : y }; ... #elif defined(MAC_OS_X) GENERIC_MAX(float) ... #else becomes #error no OS specified #endif float float_max(float x, float y) { return x > y ? x : y }; 11 12 Some General Properties Summary • A macro may contain another macro • Preprocessing allows programmers to modify C source A Preprocessor will rescan to replace code automatically A How many times does the preprocessor rescan? A File inclusion Conditional compilation • A macro definition is in effect until the end of the file A A Macros • A macro may not be defined twice • Macros are sometimes useful, but you need to be careful • You can use “#undef” to undefine a macro A Make sure that you remember the rules A Must use parentheses for the arguments A Avoid side effects A Can improve readability A Can reduce the amount of code A Overuse can also reduce readability, or introduce opportunities for bugs 13 14.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us