为什么编译器会抱怨这个例子?

时间:2012-09-22 06:31:45

标签: c

我正在维基百科上阅读Typedef。该页面上提到的示例是

typedef int km_per_hour ;
typedef int points ;

km_per_hour current_speed ;
points high_score ;
...

void congratulate(points your_score) {
if (your_score > high_score)
...

进一步说这是我无法理解为什么?

void foo() {
unsigned int a;         // Okay
unsigned km_per_hour b; // Compiler complains
long int c;             // Okay
long km_per_hour d;     // Compiler complains
...

为什么编译器会抱怨unsigned和long?

4 个答案:

答案 0 :(得分:3)

您不能在signed'd类型之前添加unsignedtypedefsignedunsigned只能修改基本整数类型并直接执行此操作。

编译器单独或靠近signedunsignedcharshort解析intlong。在所有其他情况下,它们被视为无效/意外/错位。

从这个意义上说,signedunsigned不能用作constvolatile修饰符。

答案 1 :(得分:1)

无法像typedefs那样修改或使用

macros。您km_per_hourint,因此只能这样使用!

您应该更改typedefs的定义或定义新的定义以消除编译器问题。

答案 2 :(得分:0)

您无法使用额外的说明符修改typedef。 你应该只写km_per_hour b;和km_per_hour d; 我希望这会有所帮助。

答案 3 :(得分:0)

因为你已经定义了一种类型 - 即将剑放入地面并成为你的案例。

你不能再去改变你的情况了。这在法庭和编译器中都不起作用。

对比这个比喻。