introduction to c - · pdf fileintroduction to c preprocessor instructor: yin lou 02/09/2011...
TRANSCRIPT
![Page 1: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/1.jpg)
Introduction to CPreprocessor
Instructor: Yin Lou
02/09/2011
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 2: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/2.jpg)
Preprocessor
I Commands to the compiler
I Include files, shortcuts, conditional compilation
I Command must start at beginning of line
Common Preprocessor Commands
#include
#define
#ifdef / #ifndef
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 3: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/3.jpg)
Preprocessor
I Commands to the compiler
I Include files, shortcuts, conditional compilation
I Command must start at beginning of line
Common Preprocessor Commands
#include
#define
#ifdef / #ifndef
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 4: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/4.jpg)
Running just the preprocessor
$ gcc -E main.c -o preprocessed.c
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 5: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/5.jpg)
#include: Header Files
I Includes files: Literally copy-paste
I Typically header files
Header Files
Declares
I External functions
I Variable types
I External global variables
Typically named *.h
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 6: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/6.jpg)
#include: Header Files
I Includes files: Literally copy-paste
I Typically header files
Header Files
Declares
I External functions
I Variable types
I External global variables
Typically named *.h
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 7: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/7.jpg)
#include: Header Files
I Includes files: Literally copy-paste
I Typically header files
Header Files
Declares
I External functions
I Variable types
I External global variables
Typically named *.h
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 8: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/8.jpg)
#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);
}
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 9: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/9.jpg)
#include: Header Files
project.c
#include "mylib.h"
void foo()
{
...
m = max(p, q);
...
}
gcc -o project project.c mylib.c
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 10: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/10.jpg)
#include: Header Files
project.c
#include "mylib.h"
void foo()
{
...
m = max(p, q);
...
}
gcc -o project project.c mylib.c
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 11: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/11.jpg)
#define: Macros
Blind substitution inside file
#define malloc mymalloc
#define maxsize 100
p = malloc(maxsize);
printf("Allocated %d bytes", maxsize);
is exactly the same as
p = mymalloc(100);
printf("Allocated %d bytes", 100);
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 12: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/12.jpg)
#define: Macros
Blind substitution inside file
#define malloc mymalloc
#define maxsize 100
p = malloc(maxsize);
printf("Allocated %d bytes", maxsize);
is exactly the same as
p = mymalloc(100);
printf("Allocated %d bytes", 100);
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 13: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/13.jpg)
#ifdef: Conditional compilation
project.c
#ifdef DEBUG
#include "mylib.h"
#define malloc mymalloc
#endif
...
p = malloc(100);
For debugging: gcc -DDEBUG -o project project.c mylib.cFor release: gcc -o project project.c mylib.c
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 14: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/14.jpg)
#ifdef: Conditional compilation
project.c
#ifdef DEBUG
#include "mylib.h"
#define malloc mymalloc
#endif
...
p = malloc(100);
For debugging: gcc -DDEBUG -o project project.c mylib.cFor release: gcc -o project project.c mylib.c
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 15: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/15.jpg)
#ifdef: Conditional compilation
mylib.h
void *mymalloc(int size);
void myfree(void *ptr);
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 16: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/16.jpg)
#ifdef: Conditional compilation
mylib.c
#include <stdio.h>
#include <stdlib.h>
#include "mylib.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);
}
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 17: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/17.jpg)
#include: Problems
mylib1.h
#include "mylib2.h"
mylib2.h
#include "mylib1.h"
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 18: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/18.jpg)
#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
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 19: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/19.jpg)
#define: More useage
project.c
#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!!
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 20: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/20.jpg)
#define: More useage
project.c
#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!!
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 21: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/21.jpg)
#define: Solution
project.c
#define prod(a, b) (prod2((a), (b) * 10))
prod(5, 6 + 7) => (prod2((5), (6 + 7) * 10))
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 22: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/22.jpg)
#define: More useage
project.c
#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);
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 23: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/23.jpg)
#define: More useage
project.c
#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);
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 24: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/24.jpg)
#define: Solution
project.c
#define oldfunc(a, b) do { \
newfunc1((a)); newfunc((b)); \
} while (0)
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 25: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/25.jpg)
#define: More problems
project.c
#define max(a, b) ((a) > (b) ? (a) : (b))
max(p, q) => ((p) > (q) ? (p) : (q))
max(f1(), f2()) =>((f1()) > (f2()) ? (f1()) : (f2()))Solution: Be extra careful when calling a function inside code thatcould be a #define. Always use uppercase for macros to serve asreminder.
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 26: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/26.jpg)
#define: More problems
project.c
#define max(a, b) ((a) > (b) ? (a) : (b))
max(p, q) => ((p) > (q) ? (p) : (q))
max(f1(), f2()) =>((f1()) > (f2()) ? (f1()) : (f2()))
Solution: Be extra careful when calling a function inside code thatcould be a #define. Always use uppercase for macros to serve asreminder.
Introduction to C CS 2022, Spring 2011, Lecture 8
![Page 27: Introduction to C - · PDF fileIntroduction to C Preprocessor Instructor: Yin Lou 02/09/2011 Introduction to C CS 2022, Spring 2011, Lecture 8. ... #ifdef: Conditional compilation](https://reader031.vdocument.in/reader031/viewer/2022022002/5a7bfbf77f8b9a49588c56aa/html5/thumbnails/27.jpg)
#define: More problems
project.c
#define max(a, b) ((a) > (b) ? (a) : (b))
max(p, q) => ((p) > (q) ? (p) : (q))
max(f1(), f2()) =>((f1()) > (f2()) ? (f1()) : (f2()))Solution: Be extra careful when calling a function inside code thatcould be a #define. Always use uppercase for macros to serve asreminder.
Introduction to C CS 2022, Spring 2011, Lecture 8