include
play

#include Copies the contents of the specified file into Practical C - PDF document

#include Copies the contents of the specified file into Practical C Issues: the current file Preprocessor Directives, Typedefs, Multi-file < > indicate to look in a known location for Development, and Makefiles includes


  1. #include • Copies the contents of the specified file into Practical C Issues: the current file Preprocessor Directives, Typedefs, Multi-file • < > indicate to look in a known location for Development, and Makefiles includes • “ “ indicate to look in the current directory or specified path Jonathan Misurda #include <stdio.h> jmisurda@cs.pitt.edu #include “myheader.h” #define #define Macros • Textual Symbol Replacements • Textual replacements with parameters: #define PI 3.1415926535 • Good: #define MAX 10 – #define MAX(a, b) (a > b) ? a : b float f = PI; • Not so good: for(i=0;i<MAX;i++) … – #define SWAP(a,b) {int t=a; a=b; b=t;} #if Example • #if <condition that can be evaluated by the #include <stdio.h> preprocessor> int main() { • What does preprocessor know? #if 0 printf(“this is not printed\n”); – Values of #defined variables #endif – Constants printf(“This is printed\n”); return 0; }

  2. Example 2 #else #include <stdio.h> #if #define VERSION 5 … #elif int main() { … #if VERSION < 5 #else printf(“this is not printed\n”); … #endif printf(“This is printed\n”); #endif return 0; } #ifdef Example #include <stdio.h> • #if defined #define MACRO – Checks to see if a macro has been defined, but doesn’t care about the value int main() { – A defined macro might expand to nothing, but is #if defined MACRO still considered defined printf(“this is printed\n”); #endif printf(“This is also printed\n”); return 0; } #undef Shortcuts • Undefines a macro: • #if defined → #ifdef • #if !defined → #ifndef #include <stdio.h> #define MACRO #undef MACRO int main() { #if defined MACRO printf(“this is not printed\n”); #endif printf(“This is printed\n”); return 0; }

  3. Uses Notes • Handle Operating System/Architecture • Can define variables from the commandline specific code with –D – gcc –o test –DVERSION=5 test.c • Handle differences in compilers – gcc –o test –DMACRO test.c • Build program with different features – Debugging: #ifdef DEBUG printf(…) #endif Other Preprocessor Details Defined Constants Macro Meaning • # ‐ quotes a string __FILE__ The currently compiled file __LINE__ The current line number __DATE__ The current date • ## ‐ concatenates two things __TIME__ The current time __STDC__ Defined if compiler supports ANSI C … Many other compiler ‐ specific flags • #pragma • #warn • #error typedef Type Clarity typedef type ‐ declaration synonym; void takes_int(int_pointer x) void takes_array(int_array x, int n) { { *x = 3; int i; } Examples: for(i=0; i<n; i++) printf(“%d\n”, x[i]); } typedef int * int_pointer; typedef int * int_array;

  4. Structures Function Pointers #include <stdio.h> #include <stdlib.h> Typedef Struct with Instance typedef void (*FP)(int, int); void f(int a, int b) { typedef struct node { struct node { printf("%d\n", a+b) } int i; int i; struct node *next; struct node *next; void g(int a, int b) { printf("%d\n", a*b) } Node ; } Node ; } int main() { Node * head ; FP ar1 = f; FP ar2 = g; ar1(2,3); ar2(2,3); return 0; } Function Pointers As Parameters Comparator int compare_ints(const void *a,const void void qsort ( *b) void *base , { size_t num , int *x = (int *)a; size_t size , int *y = (int *)b; int (*comparator)(const void *, const void *) ); return *x ‐ *y; } Multi-file Development Static Local Scope • Want to break up a program into multiple files • Scope: Local – Easier to maintain • Lifetime: “Global” (life of program) – Multiple authors – Quicker compilation void f(…) { – Modularity static int x; … }

  5. File Scope Example • “Global Variables” are actually limited to the a.c b.c file #include <stdio.h> int x = 0; • extern maybe be used to import variables extern int x; int f(int y) int f(int); from other files { return x+y; int main() File A File B } { x = 5; int x; extern int x; printf("%d", f(0)); return 0; Will refer to the same memory location } Compiling Static gcc a.c b.c a.c b.c static int x = 0; #include <stdio.h> ./a.out extern int x; static int f(int y) int f(int); { 5 return x+y; int main() } { x = 5; printf("%d", f(0)); return 0; } Compiling Header Files gcc a.c b.c • Usually only contain declarations – Variables /tmp/cccyUCUA.o(.text+0x6): In – Functions function `main': – #defined macros : undefined reference to `x' • Paired with an implementation file /tmp/cccyUCUA.o(.text+0x19): In function `main': : undefined reference to `f' collect2: ld returned 1 exit status

  6. Including a Header File Once Headers and Implementation #ifndef _MYHEADER_H_ mymalloc.h mymalloc.c #define _MYHEADER_H_ static MallocInfo *head; void *my_nextfit_malloc(int size); void *my_nextfit_malloc(int size){ void my_free(void *ptr); …Definitions of header to only be included once static MallocInfo *current; ... } void my_free(void *ptr) { #endif ... } Driver Makefiles • Driver program: • Express what files depend upon others #include “mymalloc.h” • If any are modified, build smallest set required • Can now use those functions • Compile: gcc –o malloctest mymalloc.c mallocdriver.c Makefile Dependency Graph malloctest: mymalloc.o mallocdriver.o malloctest gcc –o malloctest mymalloc.o mallocdriver.o mymalloc.o: mymalloc.c mymalloc.h gcc –c mymalloc.c mymalloc.o mallocdriver.o mallocdriver.o: mymalloc.h mallocdriver.c gcc –c mallocdriver.c mallocdriver. mymalloc.c mymalloc.h c

Recommend


More recommend