C + + #if #endif #define #ifdef #ifndef #if defined #if!defined detailed

Source: Internet
Author: User

Tag:io    use    sp   on    file    div    bs    code    ad   

First, let's start with the beginning of the file, in a lot of header files, we will see this statement #ifndef _myheadfile_h #define _MYHEADFILE_H//.... Statement ... #endif//_myheadfile_h in order to avoid the same file being included multiple times, we often use #ifndef to determine, if not include _myheadfile_h, define a macro with a # # _myhea  Dfile_h, #endif and #ifndef Echo, said the end.  Speaking of which, we need to mention a C language preprocessor, preprocessor is a small software, it can be compiled before the C program, its behavior is controlled by pre-processing instructions, pre-processing instructions include the following: 1, macro definition   #define 2, file contains #include 3, conditional compilation #if #ifdef #ifndef #if defined #if!defined #elif #else instructions are all starting with #, let's take a look at a simple macro definition (object-type macro) #endif E   STD   replace list #define &NBSP;PI  3.1415926 can rename a type #define &NBSP;BOOL  int macros can take arguments, and they are often called macro functions #define Identifier (X1,X2 ...  The replacement list is especially notable for identifiers and (there cannot be spaces between them, and parentheses are required.) Let's take a look at the example: #define MAX (x, y) > x):(y)) #define Is_even (n) ((n)%2==0) #define TOUPPER (c) (' A ' < (c) && (c) < ' Z '? ( c)-' a ' + ' a ' (c)) #define SWAP (t,x,y) {T t=x; x=y; y=t} can also be a bit more complicated, for example, let's write a macro function that verifies whether a date is legal #define ISLEAP (y) (y)%4==0&A mp;& (y)%100!=0| | (y)%400==0) #define Issmall (M) ((m) ==4| | (m) ==6| | (m) ==9| | (m) ==11) #define NORMAL (M) (Issmall (m) 30:31) #define DAYS (Y,m) ((M) ==2?28+isleap (y): NORMAL (m)) #define IN (x, From,to) ((x)  ; = (from) && (x) <= (to)) #define VALID (Y,m,d) ((y) >1600&&in (m,1,12) &&in (D,1,days (y,m))) Let's take a look at the conditional compilation #if (comdition) {///Statement # #;} #endif if (comdition) is true, which is logic 1, compile the following statement if (Comdition) is false, that is, logic 0, the following statement is not compiled.  Examples are as follows: #define DEBUG #if Debug Printf ("Value of i:%d\n", I);  Printf ("Value of J:%d\n", j); #endif format: #if   constant expression constant expression is 0 o'clock, the preprocessor removes the middle code of # if and #endif #if will see no defined standard character as 0, if debug is not defined, test # If debug will fail, but # if!  Debug will succeed. You can define A file name with a macro: #if define (IA32) #define Cpu_file "ia32.h" #elif defined (IA64) #deifine cpu_file "ia64.h" #elif defined (A MD64) #define Cpu_file "Amd64.h" #endif #include cpu_file can also cancel macros that have already been defined: #if defined VALUE              //Checks if value is defined, if defined #undef value            //dismiss statement definition #define Value            //  Redefine value to #endif if the test is not defined, you can write: #ifndef value              //If VALUE is not defined  #define VALUE          //  defines the macro in which value is #endif above: #undef为解除定义;  #ifndef是if the abbreviation for not defined can also be written as # if!defined if not defined;  #ifdef是if defined abbreviations can also be written as # if defined that the check is defined; The difference between #ifdef and #if defined, #ifndef is similar to the difference between # if!defined, in that the latter can form a complex precompiled condition that only determines whether a single macro is defined, for example: #if defined (perl_pack_can_ shrieksign)//* V/SIZE16, #else 0, #endif #ifdef perl_pack_can_shrieksign/* v */SIZE16, #else 0, #endif #ifd  EF is shorthand, but it does not support more complex expressions. #ifdef Have_myheader # if VERSION > 3 ... # endif #endif this case with #if defined (have_myheader) && VERSION > 3. #endif more Reasonable

C + + #if #endif #define #ifdef #ifndef #if defined #if!defined detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.