Define and typedef differences

Source: Internet
Author: User

The time of the function is different

The typedef works in the compile phase, so there is the function of type checking.

define in the preprocessing phase function ( before compilation), with a simple string substitution without type check.


Second, different Functions

typedef

(1) aliases for defining types

(2) Define machine-independent types

(for example, to define a floating-point type called REAL, it can get the highest precision on the target machine: typedef long double REAL;
So long is not supported On a double machine, the typedef would look like this: typedef double REAL;
even double On a machine that is not supported, the typedef looks like this: typedef float REAL;)


Define

(1) alias the type

(2) define constants, variables, compilation switches


Third, different scopes

#define没有作用域的限制, as long as it is a previously predefined macro, can be used in future programs. And the typedef has its own scope.


Four, manipulation of pointers

<span style= "FONT-SIZE:14PX;" >typedef int * pint;  #define PINT int * </span>
<span style= "FONT-SIZE:14PX;" >const Pint p;//p cannot be changed, p-pointing content can be changed, equivalent to int * const p;  Const PINT p;//p can be changed, p-pointing content cannot be changed, equivalent to const int *P, or int const *p;  PINT s1, S2;//equivalent to <span style= "font-family: ' Times New Roman '; line-height:19.5px; Background-color:rgb (250, 247, 239); " >int *a; int *b;</span>  i.e. both are pointers pint S3, S4;///equivalent to <span style= "font-family: ' Times New Roman '; line-height:19.5px; Background-color:rgb (250, 247, 239); " >int *a, B; Only the former is the pointer </span></span>


For more details, see "the detailed differences between typedef and define"

Define and typedef differences

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.