Unformatted text preview:

The Preprocessor CS 2022 Introduction to C Instructor Hussam Abu Libdeh Cornell University based on slides by Saikat Guha Fall 2011 Lecture 9 The Preprocessor CS 2022 Fall 2011 Lecture 9 Preprocessor I I I Commands to the compiler Include files shortcuts conditional compilation Command must start at beginning of line Common preprocessor commands I I I include define ifdef ifndef The Preprocessor CS 2022 Fall 2011 Lecture 9 include Header Files I I Includes files Literally copy paste Typically header files Header File Declares I External functions I Variable types I External global variables Typically named h or sometimes hpp for C The Preprocessor CS 2022 Fall 2011 Lecture 9 include Header Files mylib h int max int a int b mylib c include mylib h int max int a int b return a b a b The Preprocessor CS 2022 Fall 2011 Lecture 9 include Header Files project c include mylib h void foo m max p q gcc o project project c mylib c The Preprocessor CS 2022 Fall 2011 Lecture 9 Running just the preprocessor To see the resulting code after preprocessing gcc E o preprocessed c project c The Preprocessor CS 2022 Fall 2011 Lecture 9 define Macros Blind substitution inside file define define malloc maxsize mymalloc 100 p malloc maxsize printf Allocated d bytes maxsize is exactly the same as p mymalloc 100 printf Allocated d bytes 100 The Preprocessor CS 2022 Fall 2011 Lecture 9 ifdef Conditional compilation project c ifdef DEBUG include mylib h define malloc define free endif mymalloc myfree p malloc 100 For debugging gcc DDEBUG o project project c mylib c For release gcc o project project c mylib c The Preprocessor CS 2022 Fall 2011 Lecture 9 ifdef Conditional compilation mylib h void mymalloc int size void myfree void ptr The Preprocessor CS 2022 Fall 2011 Lecture 9 ifdef Conditional compilation mylib c include stdio h include stdlib h void mymalloc int size void ret malloc size fprintf stderr Allocating d at p n size ret return ret void myfree void ptr fprintf stderr Freeing p n ptr free ptr The Preprocessor CS 2022 Fall 2011 Lecture 9 include Problems mylib1 h include mylib2 h mylib2 h include mylib1 h The Preprocessor CS 2022 Fall 2011 Lecture 9 include Solution mylib1 h ifndef MYLIB1 H define MYLIB1 H include mylib2 h endif mylib2 h ifndef MYLIB2 H define MYLIB2 H include mylib1 h endif The Preprocessor CS 2022 Fall 2011 Lecture 9 define More useage I Use define to create inline functions I I I I Look like functions but are not Expanded by prerpocessor into code Makes code more readable More efficient than actual function call I No function call stack frame define INTS n int malloc n sizeof int int arr INTS 5 int arr int malloc 5 sizeof int The Preprocessor CS 2022 Fall 2011 Lecture 9 define More useage I CAUTION Macros get expanded as is Might cause syntax issues or unwanted program bugs define prod a b prod2 a b 10 prod 5 6 prod2 5 6 10 prod 5 6 7 prod2 5 6 7 10 BUG The Preprocessor CS 2022 Fall 2011 Lecture 9 define Solution define prod a b prod2 a b 10 prod 5 6 7 prod2 5 6 7 10 The Preprocessor CS 2022 Fall 2011 Lecture 9 define More useage define oldfunc a b newfunc1 a newfunc2 b oldfunc 5 6 newfunc1 5 newfunc2 6 for i 0 i 5 i oldfunc 5 6 for i 0 i 5 i newfunc1 5 newfunc2 6 BUG The Preprocessor CS 2022 Fall 2011 Lecture 9 define Solution define oldfunc a b do newfunc1 a newfunc b while 0 for i 0 i 5 i oldfunc 5 6 for i 0 i 5 i do newfunc1 5 newfunc2 6 while 0 The Preprocessor CS 2022 Fall 2011 Lecture 9 define More problems define max a b a b a b max p q p q p q max f1 f2 f1 f2 f1 f2 BUG Solution Be extra careful when calling a function inside code that could be a define Always use uppercase for macros to serve as reminder The Preprocessor CS 2022 Fall 2011 Lecture 9


View Full Document

CORNELL CS 2022 - The Preprocessor

Loading Unlocking...
Login

Join to view The Preprocessor and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view The Preprocessor and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?