C Language Basics Tutorial (iv) Pointers, structures, unions, and enumerations (19)

Source: Internet
Author: User
Tags definition constant expression include numeric min numeric value printf

preprocessing directives
As stipulated by ANSI standards, preprocessing directives mainly include:
#define
#error
#if
#else
#elif
#endif
#ifdef
#ifndef
#undef
#line
#pragma
As can be seen from the above instructions, each preprocessing instruction has a symbol "#". Here are just a few common instructions.
1. #define指令
#define指令是一个宏定义指令, the general form of the definition is:
#define A macro substitution name string (or numeric value)
Once defined by the #define directive, it is replaced with a defined string (or numeric value) each time the macro substitution name is encountered in the program.
For example, use the following statement to define true to indicate a value of 1, false to 0.
#define TRUE 1
#define FALSE 0
Once you have used true and false in the source program, the compilation is automatically replaced with 1 and 0.
Attention:
1. There is no ";" After the name of the macro definition.
2. In the Turbo C program, it is customary to replace names with uppercase characters as macros and often at the beginning of a program.
3. A macro definition also has the feature that a macro substitution name can have formal parameters that, when used in a program, replace these formal parameters.
For example:
#define MAX (x, y) (x>y)? x:y
Main ()
{
int i=10, j=15;
printf ("The Maxmum is%d", MAX (i, j);
}
The last example of a macro definition statement is to replace the name Max (x,y) with a macro instead of the larger of the x,y, and also to define:
#define MIN (X,y) (x<y) x:y
Represents the substitution of a macro for min (x,y) to replace the lesser of the x,y.
2. #error指令
This instruction is used for program debugging, and it stops compiling when the #error instruction is encountered in the compilation. Its general form is:
#error error message
The error message is not quoted, and when the compiler encounters this instruction, it displays the following information and stops compiling.
Fatal:filename linename error directive
3. #include directives
The purpose of the #include directive is to instruct the compiler to embed another source file that is indicated by the directive in the program in which the #include directive resides, and the file should be enclosed in double quotes or angle brackets. The header file of the Turbo C library function is generally indicated by the #include instruction in the program switch.
For example:
#include <stdio.h>
Programs also allow embedding of other files, such as:
Main ()
{
#include }
Where Help.c is another file, the content can be
printf ("Glad to meet for you here!");
The example above compiles to find the embedded file by the included file path specified in the Options/directories/include directories of the integrated development environment.

4. #if, #else, #endif指令
#if, #els和 #endif instruction is conditional optional instruction, its general form is:
#if constant expression
Statement section;
#else
Statement section;
#endif
The meaning of the above structure is: if the constant expression after the #if instruction is true, compile the program segment between #if and #else, or compile the program segment between #else and #endif.
For example:
#define MAX 200
Main ()
{
#if max>999
printf ("Compiled for bigger\n");
#else
printf ("Compiled for Small\n");
#endif
}

5. #undef指令
#undef指令用来删除事先定义的宏定义, its general form is:
#undef a macro substitution name
For example:
#define TRUE 1
...
#undef ture
#undef主要用来使宏替换名只限定在需要使用它们的程序段中.

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.