In C # If, # ifdef

Source: Internet
Author: User

Site: http://www.chineselinuxuniversity.net/articles/52653.shtml

Generally, all the rows in the source program are compiled. However, sometimes you want to compile a part of the content only when certain conditions are met.

The most common form of Conditional compilation commands is:

# Ifdef identifier

Procedure 1

# Else

Procedure 2

# Endif

Its function is: when the identifier has been defined (usually defined using the # define command), compile the program segment 1; otherwise, compile the program segment 2.

The # else part can also be absent, I .e:

# Ifdef

Procedure 1

# Denif

Another form is: # If is followed by an expression instead of a simple identifier:

# If expression

Procedure 1

# Else

Procedure 2

# Endif

It is used to compile program segment 1 when the specified expression value is true (non-zero); otherwise, compile program segment 2. you can specify certain conditions in advance so that the program can execute different functions under different conditions.

For example, enter a line of letters, set the condition for compiling as needed, so that all letters can be changed to uppercase for output, or all letters can be changed to lowercase for output.

# Define Letter 1

Main ()

{

Char STR [20] = "C Language", C;

Int I = 0;

While (C = STR [I])! = '\ 0 '){

I ++;

# If letter

If (C> = 'A' & C <= 'Z') C = "c-32 ";

# Else

If (C> = 'A' & C <= 'Z') C = "C" + 32;

# Endif

Printf ("% C", C );

}

}

Running result: C Language

Now let's first define letter as 1. In this way, the first if statement is compiled because letter is true (non-zero) in the pre-processing condition compiling command, and the lower-case letters are changed to uppercase when running. If you change the first line of the program:

# Define letter 0

During preprocessing, the second if statement is compiled to convert uppercase letters into lower-case letters (the ASCII code difference between the upper-case letters and the corresponding lower-case letters is 32 ). The running status is as follows:

C Language

# Ifndef in the header file is very important. For example, you have two C files, both of which include the same header file. During compilation, these two C files need to be compiled into a runable file together, so the problem arises and a large number of declarations conflict. Put the header file content in # ifndef and # endif. Whether or not your header file will be referenced by multiple files, you must add this. The general format is as follows: # ifndef <identifier>

# Define <ID> ......

# Endif <identifier> can be freely named theoretically, but the "identifier" of each header file should be unique. The naming rule of the identifier is generally that the header file name is in uppercase, followed by an underscore, and the "." In the file name is also underlined, such as: stdio. h # ifndef _ stdio_h _

# DEFINE _ stdio_h _......

# Endif

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.