Introduction and essential difference between typedef and define

There is a difference between Typedef and define that can all be used to alias an object. This article through the introduction of typedef and define, to give readers a detailed explanation of the existence of their essential differences for reference.

Typedef is a type definition keyword that is used in computer programming languages ​​to declare custom data types and to match a variety of legacy data types to simplify programming. #define is a preprocessor directive. Let's look at it together.

Typedef is a C language statement whose function is to take "alias" for existing data types.

E.g:

Typedef int INTEGER;

This can be used INTEGER instead of int for the type of integer variable description, such as:

INTEGER a,b;

Defining arrays, pointers, structures, and other types with typedefs will bring great convenience. Not only will the writing of the program be simpler but it will make the meaning clearer, thus enhancing readability. E.g:

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

Then you can use a to describe the variables, such as:

a s1,s2;// is completely equivalent to:int s1[10],s2[10] ;

Similarly, typedef void (*p)(void) indicates that p is a type of pointer to void!

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

#define int PARA

The int where the source program is located will be replaced by PARA as it is!

Such as: the program int a, b; will be replaced before the compilation PAPA a, b;

#define is the syntax defined in C, and typedef is the syntax defined in C++. Both are common in C++, but #define becomes a precompiled directive and typedef is treated as a statement. Both Typedef and define can be used to alias an object, but the two are quite different.

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

The keyword typedef is valid at the compile stage. Because it is at compile time, typedefs have type checking functionality.

Define is a macro definition. It happens in the preprocessing stage. Before compiling, it only performs simple and mechanical string substitution without any checking.

#define usage example:

#define f(x) x*x

Main( )

{

Int a=6,b=2,c;

c=f(a)/f(b);

Printf("%d n",c);

}

The output of the program is: 36. The root cause is that #define is simply a string replacement and the parentheses "(X*X)" should be added.

2. Different functions

Typedefs are used to define aliases for types. These types not only contain internal types (int, char, etc.), but also include custom types (such as structs) that can be used to make the type easier to remember.

Such as:

Typedef int (*PF) (const char *, const char *);

Define a pointer to the function of the data type PF, where the function returns an int, the parameter is const char *.

Another important use of typedef is to define machine-independent types. For example, you can define a floating-point type called REAL, which can achieve the highest precision on the target machine:

Typedef long double REAL;

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

Typedef double REAL;

And, on machines that don't even support doubles, the typedef looks like this:

Typedef float REAL;

#define is not just an alias for a type, but you can also define constants, variables, compile switches, and so on.

3. Different scopes

#define does not have a scope limit, as long as it is a previously defined macro, can be used in future programs. Typedef has its own scope.

Void fun()

{

#define A int

}

Void gun()

{

//Also can use A here, because macro substitution has no scope,

//But if you use a typedef above, you can't use A here, but generally don't use typedef inside a function

}

4. The operation of the pointer

When the two modify the pointer type, the role is different.

Typedef int * pint;

#define PINT int *

Const pint p;/ / p can not be changed, p can point to the content can be changed, equivalent to int * const p;

Const PINT p ;/ / p can be changed, p can not change the content, equivalent to const int * p; or int const * p;

Pint s1, s2; //s1 and s2 are pointers of type int

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 not the same, I hope you do not ignore this. Through the analysis of this article, I believe you have understood the difference between the two. Having mastered the differences, it will be more flexible to use.

Ego E cigarette

Suizhou simi intelligent technology development co., LTD , https://www.msmvape.com

Posted on