本文介绍了操作和功能差异 - #define和typedef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在功能方面是否存在差异 , #define 和 typedef? 上面我指的是通过运行下面两个版本(1)和(2)得到的结果 的C代码是不同的? 例如, (1)在一个C代码中,它有: #define u_ unsigned #define uc_ unsigned char (2)重写相同的C代码to: typedef unsigned u_; typdef unsigned char uc_; TIA。 O PlamerasAre there differences in terms of functionalityof,#defineandtypedef ?By the above I mean any instance when the outcomeobtained by running two versions (1) and (2) belowof C codes are different ?For example,(1) In one C code, it has:#define u_ unsigned#define uc_ unsigned char(2) The same C code is re-writtened to:typedef unsigned u_;typdef unsigned char uc_;TIA.O Plameras推荐答案 O Plameras< os **** @ acay.com.au>写道:O Plameras <os****@acay.com.au> writes:在功能方面是否存在差异 #define 和 typedef? 上面我的意思是当通过运行C代码下面的两个版本(1)和(2)获得的结果不同时的任何实例? 例如, (1)在一个C代码中,它有: #define u_ unsigned #define uc_ unsigned char (2 )相同的C代码被重写为: typedef unsigned u_; typdef unsigned char uc _; Are there differences in terms of functionality of, #define and typedef ? By the above I mean any instance when the outcome obtained by running two versions (1) and (2) below of C codes are different ? For example, (1) In one C code, it has: #define u_ unsigned #define uc_ unsigned char (2) The same C code is re-writtened to: typedef unsigned u_; typdef unsigned char uc_; #define创建一个宏。它在每个点使用时都会扩展。 它被使用了。大多数语法检查和解析都是在*宏扩展后发生的。 typedef为类型创建别名。宏和typedef可以类似地使用,但它们确实是完全不同的东西。 例如,给定: #define u_ unsigned 然后你可以声明 _u long x; 因为_u扩展为无符号在类型名称之前 " unsigned long"解析。 一般来说,typedef比用于定义类型别名的宏更好, 因为这是他们的设计目的,并且因为 类型声明的语法是这样的,宏不总是起作用。 在这种特殊情况下,我会推荐既不使用宏也不使用 typedef。阅读代码的人会知道什么是unsigned 和unsigned char。意思。他不知道u_是什么和uc_意思是 除非他查找你的typedef。 名字unsigned和unsigned char非常清楚;有没有为他们发明其他名字的好处。 (保存击键 并没有太大的好处,特别是当它使你的代码更难以理解时。) - Keith Thompson(The_Other_Keith) ks***@mib.org < http: //www.ghoti.net/~kst> 圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst> 我们必须做点什么。这是事情。因此,我们必须这样做。#define creates a macro. It''s expanded literally at each point whereit''s used. Most syntax checking and parsing occurs *after* macros areexpanded.A typedef creates an alias for a type. Macros and typedefs can beused similarly, but they''re really completely different things.For example, given:#define u_ unsignedyou can then declare_u long x;because the "_u" expands to "unsigned" before the type name"unsigned long" is parsed.In general, typedefs are better than macros for defining type aliases,because that''s what they''re designed for, and because the syntax oftype declarations is such that a macro isn''t always going to work.In this particular case, I''d recommend using neither macros nortypedefs. Someone reading your code is going to know what "unsigned"and "unsigned char" mean. He won''t know what "u_" and "uc_" meanunless he looks up your typedef.The names "unsigned" and "unsigned char" are perfectly clear; there''sno benefit in inventing other names for them. (Saving keystrokesisn''t much of a benefit, especially when it makes your code moredifficult to understand.)--Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>We must do something. This is something. Therefore, we must do this. Keith Thompson写道:Keith Thompson wrote:在这个特殊情况下,我'' d建议既不使用宏也不使用typedef。阅读代码的人会知道什么是unsigned和unsigned char。意思。他不知道u_是什么和uc_意思是除非他查找你的typedef。 In this particular case, I''d recommend using neither macros nor typedefs. Someone reading your code is going to know what "unsigned" and "unsigned char" mean. He won''t know what "u_" and "uc_" mean unless he looks up your typedef. 你觉得使用typedef确保整个 平台的一致性, 与建立int32_t和int64_t类型的情况一样? (当然, 需要 将你的typedef定义放在预处理器条件中 确定 的性质平台......) -bluejackHow do you feel about using typedefs to ensure consistency acrossplatforms,as in the case of establishing int32_t and int64_t types? (Naturally,that requiresputting your typedef definitions within preprocessor conditionals thatascertainthe nature of the platform...)-bluejack O Plameras写道:O Plameras wrote: 在功能方面是否存在差异, #define 和 typedef? Are there differences in terms of functionality of, #define and typedef ? / * BEGIN new.c * / #define VOID_POINTER void * typedef void * void_pointer; int main(无效) { void_pointer a,b; VOID_POINTER c,d; / *这行不会编译* / 返回0; } / * END new.c * / - pete/* BEGIN new.c */#define VOID_POINTER void *typedef void * void_pointer;int main(void){void_pointer a, b;VOID_POINTER c, d; /* This line won''t compile */return 0;}/* END new.c */--pete 这篇关于操作和功能差异 - #define和typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-31 21:19