gpt4 book ai didi

c - 提供下面提到的 typedef 和 #define 行为差异的原因

转载 作者:太空狗 更新时间:2023-10-29 16:13:13 29 4
gpt4 key购买 nike

我已经浏览了可用的现有信息

difference between typedef and define & typedef,#define

所有回复都非常有用。我刚遇到一个例子,任何人都可以提供这种行为的原因。

对于声明 unsigned int 变量,这将起作用:

#define INTEGER int
unsigned INTEGER i = 10;

但是,它在 typedef 的情况下不起作用,

例如

typedef int INTEGER;
unsigned INTEGER i = 10;

它会抛出错误:'i' undeclared (first use in this function)

提前致谢!

最佳答案

预处理器只是进行复制和粘贴。当你写:

#define INTEGER int
unsigned INTEGER i = 10;

在编译器本身之前,预处理器将代码放入:

unsigned int i = 10;

然而,typedef由编译器处理。

C11 (n1570), § 6.7.8 Type definitions

A typedef declaration does not introduce a new type, only a synonym for the type so specified. That is, in the following declarations:

typedef T type_ident;
type_ident D;

type_ident is defined as a typedef name with the type specified by the declaration specifiers in T (known as T), and the identifier in D has the type ‘‘derived-declarator- type-list T ’’ where the derived-declarator-type-list is specified by the declarators of D.

换句话说,<INTEGER><int> 的同义词.但是<unsigned INTEGER>被视为不存在的单一类型。

关于c - 提供下面提到的 typedef 和 #define 行为差异的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15161609/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com