简体   繁体   English

typedef 如何在内部工作(在编译器级别)?

[英]how typedef works internally (at compiler level)?

i come across this question in an interview recently.我最近在一次采访中遇到了这个问题。 they asked me when we create new user defined Type using Typedef (Like typedef unsigned char CHAR) what happens internally??他们问我,当我们使用 Typedef(如 typedef unsigned char CHAR)创建新的用户定义类型时,内部会发生什么?

Internally there will happen nothing because it is only the information for the compiler that you introduced some alias for another type.在内部不会发生任何事情,因为它只是编译器的信息,您为另一种类型引入了一些别名。

According to the C Standard (6.7.8 Type definitions)根据 C 标准(6.7.8 类型定义)

  1. ...A typedef declaration does not introduce a new type, only a synonym for the type so specified. ... typedef 声明不会引入新类型,而只是所指定类型的同义词。

Take into account that opposite to C++ in C there is no such a notion as the user defined type.考虑到与 C 中的 C++ 相反,没有用户定义类型这样的概念。 The C Standard does not use such a term. C 标准没有使用这样的术语。

To define a user type you have to specify its behaviour.要定义用户类型,您必须指定其行为。 The C language does not allow to specify behaviours of types.:) C 语言不允许指定类型的行为。:)

As for the "memory level" then the only consequence is that you can forget what actual type this typedef alias denotes.:)至于“内存级别”,那么唯一的后果是您可以忘记这个 typedef 别名表示的实际类型。:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM