Home  >  Article  >  What is the difference between typedef and define in C language?

What is the difference between typedef and define in C language?

清浅
清浅Original
2019-04-04 09:54:087951browse

The difference between typedef and define is: define is a preprocessing editor, and the definable macro has the possibility of being replaced, while typedef is processed by the editor, and follows the scope rules, and can be used as a definition type alias

#define is a C instruction, which is also an alias for defining various data types similar to typedef. However, there are still differences between them. Next, I will introduce the differences between them in detail in the article. It has a certain reference function and I hope it will be helpful to everyone.

What is the difference between typedef and define in C language?

[Recommended course: C Language Tutorial]

1. Preprocessor VS compiler

#define is determined by the preprocessor Handled by the processor, it copies and pastes the #define value from the point of definition to the point of use. Typedefs are handled by the compiler and are the actual definitions of new types. By the time control reaches the compiler, all #defines will have been replaced.

The impact of the difference

(1) typedef should end with a semicolon but #define should not end with a semicolon

(2) There may be side effects of substitution in the #define, for example:

   typedef char * string_t;         
   #define string_d char *        
   string_t s1,s2; // s1和s2都是char *类型        
   string_d s3,s4; // s3是char *但是s4的类型是char(而不是char *)

The problem in the second declaration is because the preprocessor will replace it with

char * s3,s4;

which means s3 is of type char* , but s4 will be of type char. If you want all variables to be pointer types, all variables must specify *

(3) typedef follows the scope rules. That is, if a new type is defined in a scope (within a function), the new type name will be displayed only if the scope exists. But when the preprocessor encounters a #define, it replaces all occurrences (no scoping rules after that). For example:

 int main (){            
 { 
 //新范围开始              
 typedef int myInt_t;                 
 #define myInt_d int                
 myInt_t a;  // a的类型为int                
 myInt_d b;  // b的类型为int            
 } //新范围结束           
 myInt_t c; //错误,输入myInt_t未找到            
 myInt_d d; //d的类型为int        
 }

2, macro VS type alias

#define can also be used to define macros, but typedef can only be used to provide a new name for an existing type (it New types cannot be created). Similarly, #define can be used to define the variable

#define N 10

which will not actually define N, but will replace N with 10 throughout the code. So it can be used for named constants. Typedef can only provide new names for defined types

3. Use typedef as a type alias

Some type definitions can only be defined using typedef and cannot be used #define definition. Example:

(1) Assign a new name to the integer array of size 10

       typedef int arr [ 10 ] ;

(2) Assign a new name to the structure type

typedef struct {           
int a;            
char b;        
} myType;

Summary: The above is this This is the entire content of this article, I hope it will be helpful to everyone.

The above is the detailed content of What is the difference between typedef and define in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn