Detailed differences between typedef and define

Source: Internet
Author: User

TypedefIt is a type-defined keyword that is used to declare Custom Data Types in computer programming languages and used with various original data types to simplify programming. #DefineIs a preprocessing command. Let's take a look.

Typedef is a C language statement that allows you to take an "alias" for an existing data type ".

For example:

 
 
  1. typedef int INTEGER;  

After that, INTEGER can be used to replace int as the type of the INTEGER variable, for example:

 
 
  1. INTEGER a,b;   

Using typedef to define arrays, pointers, structures, and Other types will bring great convenience. It not only makes the program simple to write but also makes the meaning clearer, thus enhancing readability. For example:

 
 
  1. Typedef int a [10]; // indicates that a is an integer array type and the array length is 10.

Then you can use a to describe variables, such:

 
 
  1. A s1, s2; // completely equivalent to: int s1 [10], s2 [10];

Likewise, typedef void (* p) (void) indicates that p is a pointer type pointing to the void type!

# Define is a macro definition command in preprocessing, for example:

 
 
  1. #define int PARA  

Int in the source program will be replaced by PARA as is!

For example, if the program contains int a and B, it will be replaced with PAPA a and B before compilation;

# Define is the syntax defined in C, and typedef is the syntax defined in C ++. The two can be used in C ++, but # define is a pre-compiled command, typedef is treated as a statement. Both Typedef and define can be used to give an object an alias, but the two are very different.

1. First, the execution time of the two is different.

The keyword typedef is valid in the compilation phase. Because it is in the compilation phase, typedef has the type check function.

Define is a macro definition, which occurs in the preprocessing phase, that is, before compilation. It only performs simple and mechanical string replacement without any check.

# Define usage example:

 
 
  1. #define f(x) x*x  
  2. main( )  
  3. {  
  4. int a=6,b=2,c;  
  5. c=f(a) / f(b);  
  6. printf("%d \n",c);  

The output result of the program is: 36. The root cause is that # define is a simple string replacement, and should be enclosed with "X * X )".

2. Different Functions

Typedef is used to define type aliases. These types include not only internal types such as int and char, but also custom types such as struct.

For example:

 
 
  1. typedef int (*PF) (const char *, const char *); 

Define the Data Type PF of a pointer to a function. The return value of the function is int and the parameter is const char *.

Another important purpose of typedef is to define machine-independent types. For example, you can define a floating point type called REAL. On the target machine, it can obtain the highest precision:

 
 
  1. typedef long double REAL; 

On a machine that does not support long double, the typedef looks like the following:

 
 
  1. typedef double REAL; 
 

In addition, on a machine that does not support double, the typedef looks like this:

 
 
  1. typedef float REAL; 

# Define can not only take aliases for types, but also define constants, variables, and compilation switches.

3. different scopes

# Define has no scope restrictions. As long as it is a previously predefined macro, it can be used in future programs. Typedef has its own scope.

 
 
  1. Void fun ()
  2. {
  3. # Define A int
  4. }
  5. Void gun ()
  6. {
  7. // You can also use A here because macro replacement has no scope,
  8. // However, if typedef is used above, A cannot be used here, But typedef is generally not used in the function.
  9. }

4. pointer operations

They have different functions when modifying the pointer type.

 
 
  1. Typedef int * pint;
  2. # Define PINT int *
  3. Const pint p; // p cannot be changed. The content pointed to by p can be changed, which is equivalent to int * const p;
  4. Const PINT p; // p can be changed. The content pointed to by p cannot be changed, which is equivalent to const int * p; or int const * p;
  5. Pint s1, s2; // both s1 and s2 are int type pointers.
  6. PINT s3, s4; // equivalent to int * s3, s4; only one is a pointer.

In fact, the labels at the end of typedef and define are also different. I hope you will not ignore this. Through the analysis in this article, I believe you have understood the difference between the two. After understanding the difference, it will be more flexible to use.

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.