Deep understanding of C language Define

Source: Internet
Author: User
Tags php programming php programming language

In the previous article, there is a pre-compiled directive that says # include, and a common precompiled directive: #define. Its surface meaning is defined, which is usually said to "define constants", but its real function is to replace;

Such as:

#define SUCCESS 1

The whole sentence is to define a macro substitution where success is the macro name and the following is the value;

How to use:

Using success directly in the next program will represent 1. But success is not a variable, and although it looks like a constant, its value does not change. But the real constant is to follow the compiler, that is, in the later debugging you can print a constant, such as: const a=3; You can print out the value of a. But the # define thing is replaced when it is precompiled, that is, you can not see the success at compile time, it is replaced directly with 1, this is one of the differences with the const ... This is the use of macros, which is also used for macro substitution.

What is the role of # define in the following context?

Role 1: Convenient for programmers to detect.

Because in many cases, for example you may use 1, if you write directly 1, then many places may appear 1, you follow-up, see 1, it is difficult to immediately know what the meaning of the 1, in the context of the situation, so let oneself become troublesome. In this case you can use these macros to express, this can be "at a glance", as you write success, you can suddenly know that the operation is the meaning of success.

Role 2: Easy maintenance.

Such as: can be used to define the array capacity

#define MAX_SIZE 100

This can be used for subsequent use:

int a[max_size];

Later, if the capacity is small, directly modify the macro, then the use of the place is followed by the latest, because you will recompile ...

Role 3: Simplifying the wording

Such as: it can be defined as:

#define UNSIGNED short UST

Then you can write it in the following way:

UST A;

Role 4: It can actually do other languages.

Such as: Chinese programming language, easy language

such as: Here in Chinese programming:

650) this.width=650; "src=" http://p9.pstatp.com/large/2eba000214caacb77b8d "alt=" in-depth understanding of the C language define "style=" border-style:none;margin:10px auto; "/>

Execution effect:

650) this.width=650; "src=" Http://p1.pstatp.com/large/2eb9000487e239b724c8 "alt=" in-depth understanding of the C language define "style=" border-style:none;margin:10px auto; "/>

The reason for this is written in the header file:

650) this.width=650; "src=" Http://p9.pstatp.com/large/2eba000214c95dcf06e6 "alt=" in-depth understanding of the C language define "style=" border-style:none;margin:10px auto; "/>

Because, according to the previous section, you know. h is freely definable ...

This is one of the reasons why the C language can do other programming languages, such as: C language to write PHP interpreter, you can create a PHP programming language ...


This article is from the "C Language good cool" blog, please be sure to keep this source http://ningcaichen66.blog.51cto.com/2902291/1967702

Deep understanding of C language Define

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.