typedef和#在c中定义相同吗?

时间:2022-11-25 10:48:35

I wonder if typedef and #define are the same in ?

我想知道typedef和#define在c中是否相同?

9 个解决方案

#1


97  

No.

不。

#define is a preprocessor token: the compiler itself will never see it.
typedef is a compiler token: the preprocessor does not care about it.

#define是一个预处理器令牌:编译器本身永远不会看到它。typedef是一个编译器令牌:预处理器不关心它。

You can use one or the other to achieve the same effect, but it's better to use the proper one for your needs

您可以使用其中一个或另一个来实现相同的效果,但是最好使用适当的一个来满足您的需要

#define MY_TYPE int
typedef int My_Type;

When things get "hairy", using the proper tool makes it right

当事情变得“多毛”时,使用合适的工具会使它变得正确

#define FX_TYPE void (*)(int)
typedef void (*stdfx)(int);

void fx_typ(stdfx fx); /* ok */
void fx_def(FX_TYPE fx); /* error */

#2


197  

typedef obeys scoping rules just like variables, whereas define stays valid until the end of the file (or until a matching undef).

typedef遵守范围规则就像变量一样,而define直到文件结束(或者直到匹配的undef为止)才有效。

Also, some things can be done with typedef that cannot be done with define.

另外,有些事情可以用typedef来完成,而不能用define来完成。

Examples:

例子:

typedef int* int_p1;
int_p1 a, b, c;  // a, b, and c are all int pointers.

#define int_p2 int*
int_p2 a, b, c;  // only the first is a pointer!

.

typedef int a10[10];
a10 a, b, c; // create three 10-int arrays

.

typedef int (*func_p) (int);
func_p fp // func_p is a pointer to a function that
          // takes an int and returns an int

#3


19  

No, they are not the same. For example:

不,它们不一样。例如:

#define INTPTR int*
...
INTPTR a, b;

After preprocessing, that line expands to

经过预处理后,该线展开为

int* a, b;

Hopefully you see the problem; only a will have the type int *; b will be declared a plain int (because the * is associated with the declarator, not the type specifier).

希望你们能看到问题;只有a具有类型int *;b将被声明为纯int(因为*与声明符关联,而不是类型说明符)。

Contrast that with

与之对比的是

typedef int *INTPTR;
...
INTPTR a, b;

In this case, both a and b will have type int *.

在这种情况下,a和b都有类型int *。

There are whole classes of typedefs that cannot be emulated with a preprocessor macro, such as pointers to functions or arrays:

有很多类型的typedef不能用预处理器宏来模拟,比如指向函数或数组的指针:

typedef int (*CALLBACK)(void);
typedef int *(*(*OBNOXIOUSFUNC)(void))[20]; 
...
CALLBACK aCallbackFunc;        // aCallbackFunc is a pointer to a function 
                               // returning int
OBNOXIOUSFUNC anObnoxiousFunc; // anObnoxiousFunc is a pointer to a function
                               // returning a pointer to a 20-element array
                               // of pointers to int

Try doing that with a preprocessor macro.

尝试使用预处理器宏来实现这一点。

#4


9  

#define defines macros.
typedef defines types.

# define定义宏。typedef定义类型。

Now saying that, here are a few differences:

说到这里,有一些不同之处:

With #define you can define constants that can be used in compile time. The constants can be used with #ifdef to check how the code is compiled, and specialize certain code according to compile parameters.
You can also use #define to declare miniature find-and-replace Macro functions.

使用#define,您可以定义可以在编译时使用的常量。常量可以与#ifdef一起使用,以检查代码是如何编译的,并根据编译参数专门化某些代码。您还可以使用#define来声明微型查找和替换宏函数。

typedef can be used to give aliases to types (which you could probably do with #define as well), but it's safer because of the find-and-replace nature of #define constants.
Besides that, you can use forward declaration with typedef which allows you to declare a type that will be used, but isn't yet linked to the file you're writing in.

可以使用typedef给类型赋予别名(您也可以使用#define),但是由于#define常量的查找和替换特性,它更安全。除此之外,您还可以使用带有typedef的forward声明,它允许您声明将要使用的类型,但是还没有链接到正在编写的文件。

#5


4  

Preprocessor macros ("#define's") are a lexical replacement tool a la "search and replace". They are entirely agnostic of the programming language and have no understanding what you're trying to do. You can think of them as a glorified copy/paste mechanic -- occasionally that's useful, but you should use it with care.

预处理器宏(“#define's”)是词法替换工具,类似于“搜索和替换”。他们完全不了解编程语言,也不知道您要做什么。您可以将它们视为一个美化的复制/粘贴机制——偶尔这是有用的,但是您应该小心使用它。

Typedefs are a C language feature that lets you create aliases for types. This is extremely useful to make complicated compound types (like structs and function pointers) readable and handlable (in C++ there are even situations where you must typedef a type).

Typedefs是一个C语言特性,允许您为类型创建别名。这对于使复杂的复合类型(如结构体和函数指针)具有可读性和可移植性非常有用(在c++中,甚至在某些情况下您必须键入类型定义)。

For (3): You should always prefer language features over preprocessor macros when that's possible! So always use typedefs for types, and constant values for constants. That way, the compiler can actually interact with you meaningfully. Remember that the compiler is your friend, so you should tell it as much as possible. Preprocessor macros do the exact opposite by hiding your semantics from the compiler.

For(3):在可能的情况下,您应该总是喜欢语言特性而不是预处理器宏!所以总是对类型使用typedef,对常量使用常量值。通过这种方式,编译器实际上可以与您进行有意义的交互。请记住,编译器是您的朋友,所以您应该尽可能地告诉它。预处理程序宏通过向编译器隐藏语义来执行相反的操作。

#6


2  

They are very different, although they are often used to implement custom data types (which is what I am assuming this question is all about).

它们是非常不同的,尽管它们经常用于实现自定义数据类型(这就是我假设这个问题的全部内容)。

As pmg mentioned, #define is handled by the pre-processor (like a cut-and-paste operation) before the compiler sees the code, and typedef is interpreted by the compiler.

正如pmg所提到的,在编译器看到代码之前,#define由预处理器(比如剪切-粘贴操作)处理,而typedef由编译器解释。

One of the main differences (at least when it comes to defining data types) is that typedef allows for more specific type checking. For example,

主要的区别之一(至少在定义数据类型时)是typedef允许进行更具体的类型检查。例如,

#define defType int
typedef int tdType

defType x;
tdType y;

Here, the compiler sees variable x as an int, but variable y as a data type called 'tdType' that happens to be the same size as an int. If you wrote a function that took a parameter of type defType, the caller could pass a normal int and the compiler wouldn't know the difference. If the function instead took a parameter of type tdType, the compiler would ensure that a variable of the proper type was used during function calls.

这里,编译器将作为int变量x,但变量y数据类型称为“tdType”,恰好是相同的大小作为一个int。如果你写了一个函数,把一个参数defType类型,调用者可以通过正常的int和编译器不知道的区别。如果函数使用类型tdType的参数,则编译器将确保在函数调用期间使用适当类型的变量。

Also, some debuggers have the ability to handle typedefs, which can be much more useful than having all custom types listed as their underlying primitive types (as it would be if #define was used instead).

另外,一些调试器有处理typedef的能力,这比将所有的自定义类型都列成它们的底层原始类型要有用得多(因为如果使用#define的话)。

#7


1  

AFAIK, No.

AFAIK,不。

'typedef' helps you setup a "alias" to an existing data type. For eg. typedef char chr;

“typedef”帮助您为现有的数据类型设置“别名”。如。typedef char空空的;

#define is a preprocessor directive used to define macros or general pattern subsitutions. For eg. #define MAX 100, substitutes all occurences of MAX with 100

#define是用于定义宏或通用模式子类的预处理指令。如。#define MAX 100,用100代替MAX。

#8


1  

No.
typedef is a C keyword that creates an alias for a type.
#define is a pre-processor instruction, that creates a text replacement event prior to compilation. When the compiler gets to the code, the original "#defined" word is no longer there. #define is mostly used for macros and global constants.

不。typedef是一个为类型创建别名的C关键字。#define是一个预处理器指令,它在编译之前创建一个文本替换事件。当编译器到达代码时,原来的“#defined”单词就不再存在了。#define主要用于宏和全局常量。

#9


0  

As everyone said above, they aren't the same. Most of the answers indicate typedef to be more advantageous than #define. But let me put a plus point of #define :
when your code is extremely big, scattered across many files, it's better to use #define; it helps in readability - you can simply preprocess all the code to see the actual type definition of a variable at the place of its declaration itself.

正如上面所说的,他们不一样。大多数答案都表明typedef比#define更有利。但是,让我加上一个#define:当你的代码非常大,分散在多个文件中时,最好使用#define;它有助于可读性——您可以简单地对所有代码进行预处理,以查看变量在声明本身位置的实际类型定义。

#1


97  

No.

不。

#define is a preprocessor token: the compiler itself will never see it.
typedef is a compiler token: the preprocessor does not care about it.

#define是一个预处理器令牌:编译器本身永远不会看到它。typedef是一个编译器令牌:预处理器不关心它。

You can use one or the other to achieve the same effect, but it's better to use the proper one for your needs

您可以使用其中一个或另一个来实现相同的效果,但是最好使用适当的一个来满足您的需要

#define MY_TYPE int
typedef int My_Type;

When things get "hairy", using the proper tool makes it right

当事情变得“多毛”时,使用合适的工具会使它变得正确

#define FX_TYPE void (*)(int)
typedef void (*stdfx)(int);

void fx_typ(stdfx fx); /* ok */
void fx_def(FX_TYPE fx); /* error */

#2


197  

typedef obeys scoping rules just like variables, whereas define stays valid until the end of the file (or until a matching undef).

typedef遵守范围规则就像变量一样,而define直到文件结束(或者直到匹配的undef为止)才有效。

Also, some things can be done with typedef that cannot be done with define.

另外,有些事情可以用typedef来完成,而不能用define来完成。

Examples:

例子:

typedef int* int_p1;
int_p1 a, b, c;  // a, b, and c are all int pointers.

#define int_p2 int*
int_p2 a, b, c;  // only the first is a pointer!

.

typedef int a10[10];
a10 a, b, c; // create three 10-int arrays

.

typedef int (*func_p) (int);
func_p fp // func_p is a pointer to a function that
          // takes an int and returns an int

#3


19  

No, they are not the same. For example:

不,它们不一样。例如:

#define INTPTR int*
...
INTPTR a, b;

After preprocessing, that line expands to

经过预处理后,该线展开为

int* a, b;

Hopefully you see the problem; only a will have the type int *; b will be declared a plain int (because the * is associated with the declarator, not the type specifier).

希望你们能看到问题;只有a具有类型int *;b将被声明为纯int(因为*与声明符关联,而不是类型说明符)。

Contrast that with

与之对比的是

typedef int *INTPTR;
...
INTPTR a, b;

In this case, both a and b will have type int *.

在这种情况下,a和b都有类型int *。

There are whole classes of typedefs that cannot be emulated with a preprocessor macro, such as pointers to functions or arrays:

有很多类型的typedef不能用预处理器宏来模拟,比如指向函数或数组的指针:

typedef int (*CALLBACK)(void);
typedef int *(*(*OBNOXIOUSFUNC)(void))[20]; 
...
CALLBACK aCallbackFunc;        // aCallbackFunc is a pointer to a function 
                               // returning int
OBNOXIOUSFUNC anObnoxiousFunc; // anObnoxiousFunc is a pointer to a function
                               // returning a pointer to a 20-element array
                               // of pointers to int

Try doing that with a preprocessor macro.

尝试使用预处理器宏来实现这一点。

#4


9  

#define defines macros.
typedef defines types.

# define定义宏。typedef定义类型。

Now saying that, here are a few differences:

说到这里,有一些不同之处:

With #define you can define constants that can be used in compile time. The constants can be used with #ifdef to check how the code is compiled, and specialize certain code according to compile parameters.
You can also use #define to declare miniature find-and-replace Macro functions.

使用#define,您可以定义可以在编译时使用的常量。常量可以与#ifdef一起使用,以检查代码是如何编译的,并根据编译参数专门化某些代码。您还可以使用#define来声明微型查找和替换宏函数。

typedef can be used to give aliases to types (which you could probably do with #define as well), but it's safer because of the find-and-replace nature of #define constants.
Besides that, you can use forward declaration with typedef which allows you to declare a type that will be used, but isn't yet linked to the file you're writing in.

可以使用typedef给类型赋予别名(您也可以使用#define),但是由于#define常量的查找和替换特性,它更安全。除此之外,您还可以使用带有typedef的forward声明,它允许您声明将要使用的类型,但是还没有链接到正在编写的文件。

#5


4  

Preprocessor macros ("#define's") are a lexical replacement tool a la "search and replace". They are entirely agnostic of the programming language and have no understanding what you're trying to do. You can think of them as a glorified copy/paste mechanic -- occasionally that's useful, but you should use it with care.

预处理器宏(“#define's”)是词法替换工具,类似于“搜索和替换”。他们完全不了解编程语言,也不知道您要做什么。您可以将它们视为一个美化的复制/粘贴机制——偶尔这是有用的,但是您应该小心使用它。

Typedefs are a C language feature that lets you create aliases for types. This is extremely useful to make complicated compound types (like structs and function pointers) readable and handlable (in C++ there are even situations where you must typedef a type).

Typedefs是一个C语言特性,允许您为类型创建别名。这对于使复杂的复合类型(如结构体和函数指针)具有可读性和可移植性非常有用(在c++中,甚至在某些情况下您必须键入类型定义)。

For (3): You should always prefer language features over preprocessor macros when that's possible! So always use typedefs for types, and constant values for constants. That way, the compiler can actually interact with you meaningfully. Remember that the compiler is your friend, so you should tell it as much as possible. Preprocessor macros do the exact opposite by hiding your semantics from the compiler.

For(3):在可能的情况下,您应该总是喜欢语言特性而不是预处理器宏!所以总是对类型使用typedef,对常量使用常量值。通过这种方式,编译器实际上可以与您进行有意义的交互。请记住,编译器是您的朋友,所以您应该尽可能地告诉它。预处理程序宏通过向编译器隐藏语义来执行相反的操作。

#6


2  

They are very different, although they are often used to implement custom data types (which is what I am assuming this question is all about).

它们是非常不同的,尽管它们经常用于实现自定义数据类型(这就是我假设这个问题的全部内容)。

As pmg mentioned, #define is handled by the pre-processor (like a cut-and-paste operation) before the compiler sees the code, and typedef is interpreted by the compiler.

正如pmg所提到的,在编译器看到代码之前,#define由预处理器(比如剪切-粘贴操作)处理,而typedef由编译器解释。

One of the main differences (at least when it comes to defining data types) is that typedef allows for more specific type checking. For example,

主要的区别之一(至少在定义数据类型时)是typedef允许进行更具体的类型检查。例如,

#define defType int
typedef int tdType

defType x;
tdType y;

Here, the compiler sees variable x as an int, but variable y as a data type called 'tdType' that happens to be the same size as an int. If you wrote a function that took a parameter of type defType, the caller could pass a normal int and the compiler wouldn't know the difference. If the function instead took a parameter of type tdType, the compiler would ensure that a variable of the proper type was used during function calls.

这里,编译器将作为int变量x,但变量y数据类型称为“tdType”,恰好是相同的大小作为一个int。如果你写了一个函数,把一个参数defType类型,调用者可以通过正常的int和编译器不知道的区别。如果函数使用类型tdType的参数,则编译器将确保在函数调用期间使用适当类型的变量。

Also, some debuggers have the ability to handle typedefs, which can be much more useful than having all custom types listed as their underlying primitive types (as it would be if #define was used instead).

另外,一些调试器有处理typedef的能力,这比将所有的自定义类型都列成它们的底层原始类型要有用得多(因为如果使用#define的话)。

#7


1  

AFAIK, No.

AFAIK,不。

'typedef' helps you setup a "alias" to an existing data type. For eg. typedef char chr;

“typedef”帮助您为现有的数据类型设置“别名”。如。typedef char空空的;

#define is a preprocessor directive used to define macros or general pattern subsitutions. For eg. #define MAX 100, substitutes all occurences of MAX with 100

#define是用于定义宏或通用模式子类的预处理指令。如。#define MAX 100,用100代替MAX。

#8


1  

No.
typedef is a C keyword that creates an alias for a type.
#define is a pre-processor instruction, that creates a text replacement event prior to compilation. When the compiler gets to the code, the original "#defined" word is no longer there. #define is mostly used for macros and global constants.

不。typedef是一个为类型创建别名的C关键字。#define是一个预处理器指令,它在编译之前创建一个文本替换事件。当编译器到达代码时,原来的“#defined”单词就不再存在了。#define主要用于宏和全局常量。

#9


0  

As everyone said above, they aren't the same. Most of the answers indicate typedef to be more advantageous than #define. But let me put a plus point of #define :
when your code is extremely big, scattered across many files, it's better to use #define; it helps in readability - you can simply preprocess all the code to see the actual type definition of a variable at the place of its declaration itself.

正如上面所说的,他们不一样。大多数答案都表明typedef比#define更有利。但是,让我加上一个#define:当你的代码非常大,分散在多个文件中时,最好使用#define;它有助于可读性——您可以简单地对所有代码进行预处理,以查看变量在声明本身位置的实际类型定义。