Preprocessor Directive to Include Another File at the Current Point in the Program

Preprocessor Directive to Include Another File at the Current Point in the Program

<p>CSE230CSE230 </p><p>CC PreprocessorPreprocessor</p><p>#include </p><p>1 • Preprocessor directive to include another file at the current point in the program • #include “file.h” locate file.h in current directory (can use a full UNIX path name) • #include <file.h> locate file.h in standard include directory (usually /usr/include)</p><p>#define • Preprocessor directive that replaces every occurrence of the specified identifier by the replacement string in the remainder of the file #define PI 3.141592 #define EOF (-1) #define EQ == #define SQR(x) ______#define BAD_SQR(x) x * x #define WORSE_SQR (x) ______; #undef EQ Example • Macro that finds the maximum of 3 values #define max(x,y) ______#define max3(x,y,z) max(max(x,y),z) • If #define cannot fit on one line, use backslash at end of first line to continue macro on next line • To view results of preprocessor without compilation: gcc -E file.c</p><p>Preprocessing Operators</p><p>2 • #define greeting(m) \ printf(#m)</p><p>/* In main(): */ greeting(Hello World!); • #define X(i) x ## i /* In main(): */ ______= ______; Conditional Compilation • Allows certain sections of code to be compiled if a condition is met • #define DEBUG /* ______*/ #ifdef DEBUG /* debugging code goes here */ #endif /* ______*/ More Conditional Compilation • #if defined(SUNSPARC) /* ______*/ #else /* ______*/ #endif • #if ndef(PI) float PI = 3.141592; #endif</p><p>More Conditional Compilation(cont’d) </p><p>3 • Prevent multiple inclusion of header #ifndef SomeName #define SomeName</p><p>/* declarations and code here */</p><p>#endif</p><p>Assert • Generally used for debugging to test preconditions • assert is a macro (see page 389)</p><p>#include <assert.h> int display_die(int roll) { assert(______); /* function code goes here */ } Quicksort Preprocessor Info /* Page 391 */ #include <stdio.h> #include <time.h> /* for time() */ #include <stdlib.h> /* for rand(),srand() */</p><p>#define swap(x,y) \ { ______}</p><p>#define N 11 #define MAX 1001</p><p>Quicksort Program </p><p>4 int main(void) { double a[N]; int i; srand(time(NULL)); for (i=0; i < N; ++i) a[i] = (rand() % MAX)/10.0; quicksort(a, a+N-1); for (i=0; i < N; ++i) printf(“%lf\n”, a[i]); return 0; } Quicksort Algorithm void quicksort(int *left, int *right) { int *p, *pivot; if (left < right) { pivot = left; p = partition(left, right, pivot); quicksort(left, p-1); quicksort(p+1, right); } } Quicksort: Partition int *partition(int *left, int *right, int *pivot) { while (left <= right) { while (*left < *pivot) ++left; while (*right > *pivot) --right; if (left<right) { swap(*left,*right); ++left; --right; } } swap(*pivot,*right); return right; }</p><p>5</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 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