C language Conditional compilation and C language compilation

Source: Internet
Author: User

C language Conditional compilation and C language compilation

I have been learning C language for almost two years. I have been learning this thing, but I have never studied it seriously. In fact, it is very simple. I only blame myself for being too lazy.

Only when I learned thinking in java yesterday can I see that JAVA uses exceptions to replace the C language Conditional compilation function. Good or bad, you do not dare to make judgment.

After learning on the Internet for a while, I will summarize the following: (I mainly feel that I have written more examples than I do on the Internet)


The Preprocessing Program provides the Conditional compilation function. Different program parts can be compiled according to different conditions, thus generating different target code files. This is useful for program porting and debugging. Conditional compilation has three forms, which are described below.


First Form


The first format is:
# Ifdef identifier
Procedure 1
# Else
Procedure 2
# Endif

Its function is to compile program segment 1 if the identifier has been defined by the # define command; otherwise, program segment 2 is compiled. If no Segment 2 is available (it is null), # else in this format can be absent, that is, it can be written:
# Ifdef identifier
Program Section
# Endif


Example:

<Span style = "font-size: 24px;" >#include <stdio. h ># define num 1int main () {# ifdef numprintf ("macro defined"); # else printf ("no macro defined"); # endifgetchar (); return 0 ;} </span>

Because num has been define in the second line, Conditional compilation only compiles the part that has been defined by the macro.

Therefore, the output is macro-defined.


Second form


The second format is:
# Ifndef identifier
Procedure 1
# Else
Procedure 2
# Endif
The difference from the first form is to change "ifdef" to "ifndef ". Its function is to compile segment 1 if the identifier is not defined by the # define command. Otherwise, Segment 2 is compiled. This is the opposite of the function in the first form.

Here we will not repeat the examples.


Third form


The third format is:
# If constant expression
Procedure 1
# Else
Procedure 2
# Endif
Its function is to compile program segment 1 if the constant expression value is true (not 0). Otherwise, program segment 2 is compiled. Therefore, the program can complete different functions under different conditions.

Note that this is a constant expression, so it must be define, not int, because it is a variable.


Example:

<Span style = "font-size: 24px;" >#include <stdio. h> # define num 1 // The constant is 1 and not 0int main () {# if numprintf ("constant expression is not 0 "); # else printf ("constant expression 0"); # endifgetchar (); return 0 ;}</span>

Obviously, the result is a constant expression not 0.



What are the forms of Conditional compilation in C language?

The Preprocessing Program provides the Conditional compilation function. Can be compiled according to different conditions
Different program sections generate different target code files. This migration of Programs
Planting and debugging are very useful.
Conditional compilation has three forms:
1. First form:
# Ifdef identifier
Procedure 1
# Else
Procedure 2
# Endif
Its function is that if the identifier has been defined by the # define command
Compile; otherwise, the program segment 2 is compiled.
If no Segment 2 is available (it is null), # else in this format can be absent.
Write:
# Ifdef identifier
Program Section
# Endif

# Define NUM OK
Main (){
Struct stu
{
Int num;
Char * name;
Char sex;
Float score;
} * Ps;
Ps = (struct stu *) malloc (sizeof (struct stu ));
Ps-> num = 102;
Ps-> name = "Zhang ping ";
Ps-> sex = 'M ';
Ps-> score = 62.5;
# Ifdef NUM
Printf ("Number = % d \ nScore = % f \ n", ps-> num, ps-> score );
# Else
Printf ("Name = % s \ nSex = % c \ n", ps-> name, ps-> sex );
# Endif
Free (ps );
}
As the Conditional compilation preprocessing command is inserted in the second line of the program
Determines whether NUM is defined to compile the printf statement. In the first line of the program
A macro definition has been made for NUM, so the first printf statement should be compiled to run the result.
The student ID and score are output.
In the macro definition of the first line of the program, the definition of NUM indicates that the string is OK.
Is any string, or even no string, written:
# Define NUM
It also has the same meaning. The second program will be compiled only when the first line of the program is canceled.
Printf statement. Readers can try it on the computer.
2. Second form:
# Ifndef identifier
Procedure 1
# Else
Procedure 2
# Endif
The difference from the first form is to change "ifdef" to "ifndef ". Its functions
Yes. If the identifier is not defined by the # define command, the program segment 1 is compiled. No
The program segment 2 is compiled. This is the opposite of the function in the first form.
3. Third form:
# If constant expression
Procedure 1
# Else
Procedure 2
# Endif
Its function is to perform Block 1 if the constant expression value is true (not 0 ).
Compile; otherwise, the program segment 2 is compiled. Therefore, the program can be completed under different conditions.
Into different functions.
# Define R 1
Main (){
Float c, r, s;
Printf ("input a number :");
Scanf ("% f", & c );
# If R
R = 3.14159 * c;
Printf ("area of round is: % f \ n", r );
# Else
S = c * c;
Printf ("area of square is: % f \ n", s );
# Endif
}
The third form of Conditional compilation is used in this example. In the macro definition of the first line of the program,
The R is defined as 1. Therefore, when the condition is compiled, the constant expression value is true. Therefore
Area.
The Conditional compilation described above can also be implemented using conditional statements. But use
The statement will compile the entire source program, and the generated target code program is very long.
Use Cases... the remaining full text>

The differences between Conditional compilation and If statements in C language are applicable to different situations.

Conditional compilation is the content of the preprocessing part in the C language. It is the first part to be processed when the compiler compiles the code,

Conditional compilation contains judgment statements, such as # if, # else, # elif, and # endif.
It means that if the macro conditions match, the compiler will compile the code. Otherwise, the compiler will ignore the code and not compile it, as shown in
# Define A 0 // define A as 0
# If (A> 1)
Printf ("A> 1"); // The Compiler does not compile the statement. This statement does not generate assembly code.
# Elif (A = 1)
Printf ("A = 1"); // The Compiler does not compile the statement. This statement does not generate assembly code.
# Else
Printf ("A <1"); // The Compiler compiles this code, generates assembly code, and executes this statement.
# Endif

The if statement is not the keyword in the C language. It depends on the calculation result of the expression to execute the statement. Each branch in the statement is compiled, as shown in
# Define A 0
If (A> 1)
Printf ("A> 1"); // The Compiler compiles the statement, but the statement is not executed because A = 0
Else if (A = 1)
Printf ("A = 1"); // The Compiler compiles the statement, but the statement is not executed because A = 0
Else
Printf ("A <1"); // The Compiler compiles the statement and runs it because A = 0.

In short, Conditional compilation selectively compiles statements based on macro conditions, which are completed by the compiler during code compilation;
A conditional statement is used to selectively execute a statement based on a conditional expression. It is executed when the program is running.

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.