这里我有两个结构。首先是:
struct complex {
double real, imaginary;
};
我知道它必须以分号结尾。
但是这个有一个功能
struct complex add_complex(struct complex c1, struct complex c2) {
struct complex c3;
c3.real = c1.real + c2.real;
c3.imaginary = c1.imaginary + c2.imaginary;
return c3;
}
这里如果我不在末尾包含分号,那么编译器将不会产生错误。 为什么?
最佳答案
结构定义必须始终以分号结尾。因此,如果从 struct complex
定义中删除了 ;
,就会出现编译器错误。
第二个 (add_complex
) 不是结构定义,它是一个返回结构的函数。函数末尾没有分号。
- I mean, if I write like this struct complex add_complex() { }; then this one also true. Why?
它是“OK”,实际上取决于编译器。我使用的大多数编译器(例如 gcc 和 Micosoft 的)都允许这样做,但可以使用正确的标志显示警告/错误(将 -pedantic
添加到 gcc 将给出:警告:ISO C 不允许在函数 [-pedantic]
之外添加额外的 ';',您可以添加 -Werror
) 将其变成错误。)
关于C中的结构是否总是以分号结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15743315/