1 / 7

Programming in C Chapter 10 The Preprocessor

Programming in C Chapter 10 The Preprocessor. # include “filename” # include <filename>. The Use of #include. file2.c. # include “file2.c”. B. A. A. file2.c. file1.c. file1.c. The Use of #include(cont.). file3.c. file2.c. file1.c. A. # include “file2.c”.

Download Presentation

Programming in C Chapter 10 The Preprocessor

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming in CChapter 10 The Preprocessor

  2. #include “filename” #include <filename> The Use of #include file2.c #include “file2.c” B A A file2.c file1.c file1.c

  3. The Use of #include(cont.) file3.c file2.c file1.c A #include “file2.c” #include “file3.c” C A B file3.c file1.c file2.c

  4. Example /* powers.h */ #define sqr(x) ((x)*(x)) #define cube(x) ((x)*(x)*(x)) #define quad(x) ((x)*(x)*(x)*(x)) /*ch8_10.c*/ #include <stdio.h> #include "d:\fengyi\bkc\powers.h" #define MAX_POWER 10 void main() { int n; printf("number\t exp2\t exp3\t exp4\n"); printf("----\t----\t-----\t------\n"); for(n=1;n<=MAX_POWER;n++) printf("%2d\t %3d\t %4d\t %5d\n",n,sqr(n),cube(n),quad(n)); }

  5. #define identifier token_stringopt The Use of #define 例 #define WIDTH 80 #define LENGTHWIDTH+40 var=LENGTH*2; Means: var= 80+40 *2; #define YES 1 main() { …….. } #undef YES #define YES 0 max() {…….. } ( ) YES Previous Scope ( ) YES New scope #define YES 1 #define NO 0 #define PI 3.1415926 #define OUT printf(“Hello,World”); #define WIDTH 80 #define LENGTHWIDTH+40 var=LENGTH*2; Means: var= 80+40 *2; if(x==YES) printf(“correct!\n”); else if (x==NO) printf(“error!\n”); Means: if(x==1) printf(“correct!\n”); else if (x==0) printf(“error!\n”); #define PI 3.14159 printf(“2*PI=%f\n”,PI*2); Means: printf(“2*PI=%f\n”,3.14159*2); #define MAX MAX+10 ()

  6. #define identifier(identifier,…, identifier)token_stringopt #define with Arguments • #define S(a,b) a*b • ……….. • area=S(3,2); • means: area=3*2; #define POWER(x) x*x x=4; y=6; z=POWER(x+y); means:z=x+y*x+y; simple: #define POWER(x) ((x)*(x)) means: z=((x+y)*(x+y));

  7. #define MAX(x,y) (x)>(y)?(x):(y) ……. main() { int a,b,c,d,t; ……. t=MAX(a+b,c+d); …… } Equal:t=(a+b)>(c+d)?(a+b):(c+d); int max(int x,int y) { return(x>y?x:y); } main() { int a,b,c,d,t; ……. t=max(a+b,c+d); ……… }

More Related