[英]why typedef is not working here?
I know simple definition of typedef : 我知道typedef的简单定义:
typedef is a keyword in C to assign alternative names to types.
Following this definition I tried to implement typedef as following : 按照这个定义,我尝试实现typedef如下:
int main()
{
typedef long mylong; //as per my knowledge after this statement mylong will be treated as long
int long b; // this works fine
int mylong c; // but this gives error
}
I tried this on gcc. 我在gcc上尝试过这个。 And following is the error 以下是错误
I know this error means I didn't get actual concept of typedef. 我知道这个错误意味着我没有得到typedef的实际概念。 Can anybody please tell me where I am wrong? 谁能告诉我哪里错了?
Unlike #define
, typedef
is a mechanism for introducing a new name for a type, as opposed to a textual substitution. 与#define
不同, typedef
是一种为类型引入新名称的机制,而不是文本替换。
Recall that types long int
, int long
, and long
are three synonyms that refer to the same C type. 回想一下, long int
, int long
和long
类型是引用相同C类型的三个同义词 。 When you use typedef
, you make another synonym referring to that same type. 当您使用typedef
,您将使用另一个同义词来引用相同的类型。
When you use it like this 当你像这样使用它
mylong x = 123;
the usage is correct: mylong
is used like a name of a type. 用法是正确的: mylong
用作类型的名称。 However, when you try using it in combination with int
, like this 但是,当你尝试将它与int
结合使用时,就像这样
int mylong x = 123;
the compiler reports an error because int mylong
does not name a valid type. 编译器报告错误,因为int mylong
没有命名有效类型。 To the compiler it looks the same as if you wrote, say int float x = 5
or struct mystruct int z = ...
. 对于编译器,它看起来和你写的一样,比如说int float x = 5
或struct mystruct int z = ...
When you omit the type in C
, it's assumed int
当你省略C
的类型时,它假定为int
So typedef long mylong;
所以typedef long mylong;
is the same as typedef long int mylong;
与typedef long int mylong;
相同typedef long int mylong;
. 。
Making the offending line be something like this: 制作违规行是这样的:
int long int c;
Hence the error. 因此错误。
The typedef is a new type* (not text substitution for long
). typedef是一种新类型*(不是long
文本替换)。 So you don't need to add an int
to make a variable of that type. 因此,您无需添加int
来生成该类型的变量。 A simple mylong c;
一个简单的mylong c;
will suffice. 就足够了。
*Well, it's a bit more involved than that. *嗯,它比那更多参与。 The new type is in fact the same as a regular long int
, and the two are interchangeable. 新类型实际上与常规long int
相同,并且两者是可互换的。 But for sound domain logic, you should treat it as a new seperate type 但对于声域逻辑,您应该将其视为一种新的单独类型
According tp paragraph 2 (Constraints) of section 6.7.2 Type specifiers of the C Standard 根据第6.7.2节C标准的类型说明符的第2段(约束)
Each list of type specifiers shall be one of the following multisets... 类型说明符的每个列表应为以下多集的一个 ...
— void
— char
— signed char
— unsigned char
— short, signed short, short int, or signed short int
— unsigned short, or unsigned short int
— int, signed, or signed int
— unsigned, or unsigned int
— long, signed long, long int, or signed long int
— unsigned long, or unsigned long int
— long long, signed long long, long long int, or
signed long long int
— unsigned long long, or unsigned long long int
— float
— double
— long double
— _Bool
— float _Complex
— double _Complex
— long double _Complex
— atomic type specifier
— struct or union specifier
— enum specifier
— typedef name
As you see the C Standard does not allow to combine typedef name
with other type specifiers. 如您所见,C Standard不允许将typedef name
与其他类型说明符组合使用。
You misunderstand long
/ short
. 你误解了long
/ short
。
typedef long mylong;
You said "typedef is a keyword in C to assign alternative names to types ." 你说“typedef是C中的一个关键字,用于为类型指定替代名称。” long
is actually not type - long int
is type. long
实际上不是type - long int
是type。 C just allows you to leave out int
when you're using short
, long
or long long
So your statement is equal to C只允许你在使用short
, long
或long long
时候省略int
所以你的陈述等于
typedef long int mylong;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.