////main.c//enumeration and preprocessing////Created by Qianfeng on 15/6/3.//Copyright (c) 2015 Qianfeng. All rights reserved.//#include<stdio.h>/*enumeration: Used to declare a set of constant definitions: Enum Enumeration Name {Enumeration element 1, Enumeration element 2,...} Note 1, the C language will treat the enumeration element as an integer constant 2, the default first enumeration element is 0, the second is 1, sequentially plus 1 3, you can also change the enumeration value at the time of definition enum Season{spring,summer,autumn=5,winter}; Winter is 6 .*/#if0intMainintargcConst Char*argv[]) { //1. Define the enumeration type and then define the enumeration variables enumSeason{spring,summer,autumn,winter}; enumSeason S; //2. Defining enumeration variables while defining enumeration types enumSesson2{spring,summer,autumn,winter} s2; //3. Omitting enumeration names, defining enumeration variables enum{a,b,c} ch; CH=a;//equivalent to ch=0;Ch=2;//equivalent to Ch=c; //traversing an enumeration element for(s=spring;s<winter;s++) {printf ("%d", s); } return 0;}#endif/*pre-processing C language compilation, some special pre-processing instructions will be interpreted to produce a new source program, and then the usual compilation starts with #, and the end without the semicolon preprocessing directives mainly include: macro definition, conditional compilation, file inclusion scope: Starting from the definition of the line to the end of the file example: # Include <stdio.h> macro definitions are commonly used to define constants #define Macro name Strings #define PI 3.14 effect: When compiling preprocessing, replace all the macro names of the source program with the following string #undef macro name--the scope of the terminating macro The difference between a macro definition and a function 1, the macro definition does not involve the allocation of storage space, parameter type matching, return value problem 2, function calls execute while the program is running, and macro substitution is performed only during the compilation preprocessing phase, and all macro definitions with parameters have higher execution efficiency than functions*/ #if0#defineA 3.14//#undef A//macro definition with Parameters#defineAverage (A, b) (A+B)/2//#definePow (a) ((a) * (a))intMainintargcConst Char*argv[]) {printf ("%f\n", A); intA=average (4,6); printf ("The average is:%d\n", a); intRST = Pow (Ten)/pow (2); printf ("%d\n", RST); return 0;}#endif/*conditional Compilation of a block of code to meet certain conditions to compile #if condition 1 ... code1 .... #elif condition 2 ... code2 .... #else ... code3 ... #endif*/#if0#defineMAX 10intMainintargcConst Char*argv[]) {#ifMax==0printf ("Max is 0 .");#elifMax>0printf ("Max is greater than 0");#elseprintf ("Max is less than 0");#endif return 0;}#endif/*#if defined () and # if!defined () #if defined (macro name)//Determine if macros have been defined, define code to compile in ... code ... #endif #ifdef和 #ifndef usage with if defined The usage of () and # if!defined () is basically consistent, judging whether to define #ifdef MAX ... code ... #endif*/#if0typedefChar* String;//to alias a pointer//defines the struct and aliases, the struct name behind the struct is omitted, and the alias is at the back pointtypedefstruct { floatx; floaty;} Point;typedef Point* PP;//alias fromenumSeason {Spring, summer, autumn, winter};//alias enumeration, can be used to save the previous modifier enumtypedefenumSeason Season;intMainintargcConst Char*argv[]) {String a="1231231"; Point Point= {Ten, -}; PP P= &Point ; //enum Season s = Spring;Season s=Spring; printf ("spring=%d\n", s); printf ("%f,%f\n", POINT.X,POINT.Y); printf ("%f,%f\n",p->x,p->y); printf ("%s\n", a);#ifDefined (MAX)printf ("defined a max macro definition \ n");#elseprintf ("The max macro definition has not been defined \ n");#endif #ifdefMAX printf ("defines a Max macro definition");#elseprintf ("The max macro definition has not been defined \ n");#endif return 0;}#endif/*file contains this we have been in contact with, that is, # include, it can copy the entire contents of a file into another file #include < file name >---> Find the file directly in the directory where the C library function header file is located #include " File name "---> Header file for customization, find order first in the current directory of the source program--The path path of the operating system-->c the directory where the language library function header file is located note 1, #include允许嵌套, do not allow recursion 2, use # Include directives may result in the same file being included multiple times, reducing compilation efficiency*/
Enumeration and preprocessing