# Ifdef, # ifndef, # else, # endif execution condition compilation,

Source: Internet
Author: User

# Ifdef, # ifndef, # else, # endif execution condition compilation,
The program we developed should not only run on pc, but also on mobile. At this time, the program will execute selective compilation based on the machine environment. For example, compile the PC-side program and the mobile-side program on the Mobile End. Here we can compile the program with two sets of conditions. Let's take a look at # ifdef... # endif; Syntax format: # ifdef macro name | # ifdef macro name // any code | // any code # endif | # else | // any code | # The two formats above endif are, if a macro is specified, # ifdef... # code in endif.

//// Main. m // Hong_Test /// Created by Cheng Yingying on. // Copyright, 2017, Cheng yingmiao. all rights reserved. // # import <Foundation/Foundation. h> # define iPadint main (int argc, const char * argv []) {@ autoreleasepool {# ifdef iPad NSLog (@ "this is ipad "); # else NSLog (@ "this is iphone"); # endif // insert code here... NSLog (@ "Hello, World! ");} Return 0 ;}
The execution result is 22:54:22. 010569 Hong_Test [8061: 318230] this is ipad. Let's analyze it. First, we define macro # define iPad. Next, the program will search for the macro, the program to be compiled will be compiled according to our specific conditions. In fact, it is similar to if... else... if. But why is there such a thing? There are two things: 1. we will write a lot of statements when debugging the program. In the official deployment, we only need to remove the Macro. Second, such Conditional compilation can reduce the size of the program. Next we will use # if... # else... # endif for an instance drill to end today's learning.
//// Main. m // Hong_Test /// Created by Cheng Yingying on. // Copyright, 2017, Cheng yingmiao. all rights reserved. // # import <Foundation/Foundation. h> # define age 30int main (int argc, const char * argv []) {@ autoreleasepool {# if age> 60 NSLog (@ ""); # elif age> 40 NSLog (@ "middle-aged"); # elif age> 20 NSLog (@ ""); # endif} return 0 ;}

Result: young people

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.