C 中的常量返回类型

发布于 2024-09-15 15:45:34 字数 602 浏览 4 评论 0原文

我正在阅读一些代码示例,它们返回了 const int。当我尝试编译示例代码时,出现了有关返回类型冲突的错误。所以我开始搜索,认为 const 是问题所在(当我删除它时,代码工作正常,不仅可以编译,而且按预期工作)。但我从未能够找到专门与 const 返回类型相关的信息(我为结构/参数/等找到了信息,但没有找到返回类型)。所以我尝试编写一段代码来简单地展示 const 可以做什么。我想出了这个:

#include <stdio.h>

int main() {
    printf("%i", method());
}

const int method() {
    return 5;
}

当我编译这个时,我得到:

$ gcc first.c 
first.c:7: error: conflicting types for ‘method’
first.c:4: note: previous implicit declaration of ‘method’ was here

但是,每当我删除 const 时,它都会如预期的那样简单地打印出 5,a 继续生命。那么,谁能告诉我 const 用作返回类型时应该意味着什么。谢谢。

I was reading some samples of code, and they returned a const int. When I tried to compile the examples code I got errors concerning conflicting return types. So I started searching, thinking that the const was the problem (when I removed it, the code worked fine, not only did it compile, but worked as expected). But I never was able to find information specifically pertaining to a const return type (I did for structures/parameters/etc. etc., but not return types). So I tried writing a piece of code to simply show what const may do. I came up with this:

#include <stdio.h>

int main() {
    printf("%i", method());
}

const int method() {
    return 5;
}

And when I compile this, I get:

$ gcc first.c 
first.c:7: error: conflicting types for ‘method’
first.c:4: note: previous implicit declaration of ‘method’ was here

However, whenever I remove the const, it, as expected, simply prints out a 5, a continues on with life. So, can anyone tell me what const is supposed to mean when used as a return type. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初见你 2024-09-22 15:45:34

const 对于返回值没有任何意义,因为返回值在任何情况下都是右值并且无法修改。您收到的错误是由于您在声明函数之前使用了该函数,因此隐式假定返回 int,而不是 const int 但当该方法实际上已定义,返回类型与原始假设不匹配。如果返回 double 而不是 int,您会得到完全相同的错误。

例如:

#include <stdio.h>

int main() {
    printf("%i", method());
}

double method() {
    return 5;
}

生成:

$ gcc -std=c99 -Wall -Wextra -pedantic impl.c
impl.c: In function ‘main’:
impl.c:4: warning: implicit declaration of function ‘method’
impl.c: At top level:
impl.c:7: error: conflicting types for ‘method’
impl.c:4: note: previous implicit declaration of ‘method’ was here

看看调高警告级别有多大帮助!

const makes no sense for return values because return values are rvalues in any case and can't be modified. The error you are getting is from the fact that you use a function before it has been declared so it is implicitly assumed to return int, not const int but then when the method is actually defined, the return type doesn't match the original asssumption. You would get exactly the same error if it were, say, to return double instead of int.

E.g.:

#include <stdio.h>

int main() {
    printf("%i", method());
}

double method() {
    return 5;
}

generates:

$ gcc -std=c99 -Wall -Wextra -pedantic impl.c
impl.c: In function ‘main’:
impl.c:4: warning: implicit declaration of function ‘method’
impl.c: At top level:
impl.c:7: error: conflicting types for ‘method’
impl.c:4: note: previous implicit declaration of ‘method’ was here

See how helpful it is to turn the warning levels up!

硬不硬你别怂 2024-09-22 15:45:34

在调用 method() 之前添加 method() 的原型将修复该错误。

const int method();
int main() {
    printf("%i", method());
}

Line 7: error: conflicting types for 'method'

此错误告诉我们 method() 是由编译器创建的(因为它没有找到它),其返回类型与 const int (可能是 int)不同。

Line 4: error: previous implicit declaration of 'method' was here

这个另一个错误告诉我们,实际上编译器创建了自己的方法版本。

Adding the prototype of method() before you call it will fix the error.

const int method();
int main() {
    printf("%i", method());
}

Line 7: error: conflicting types for 'method'

This error tells us that method() was created by the compiler (because it didn't find it) with a different return type than const int (probably int).

Line 4: error: previous implicit declaration of 'method' was here

This other error tells us that in fact the compiler created its own version of method.

凉城已无爱 2024-09-22 15:45:34

main 发现使用了没有原型的 method(),因此它假设它返回 int。然后将其声明为返回const int。将 method() 的声明移到 main 之前,或者将原型放在 main 之前。

main sees a use of method() without a prototype, so it assumes it returns int. Then you declare it as returning const int. Move the declaration of method() before main, or put a prototype before main.

心舞飞扬 2024-09-22 15:45:34

您发布的代码至少应该为您提供一个未定义的标识符:method。在调用该函数之前,您需要在范围内进行声明。更好的使用:

#include <stdio.h>

const int method() {
    return 5;
}

int main() {
    printf("%i", method());
}

定义也是声明。所以,这应该可以解决您的错误。

The code you posted should give you an undefined identifier: method at the very least. You need a declaration in scope before you can call the function. Better use:

#include <stdio.h>

const int method() {
    return 5;
}

int main() {
    printf("%i", method());
}

A definition is also a declaration. So, this should fix your error.

み格子的夏天 2024-09-22 15:45:34

当您使用某个函数时,在您向 C 详细介绍该函数之前,C 会猜测该函数的返回类型——它的名称、返回类型、常量性和参数。如果这些猜测是错误的,你就会得到错误。在这种情况下,他们错了。使用原型或将函数移至调用上方。

哦,关于 CONST 性:
这意味着如果您使用相同的参数再次调用函数,该函数的值将是相同的,并且应该不会有(重要的)副作用。这对于优化很有用,而且它还以书面形式声明编译器可以强制执行相关参数。函数承诺不改变常量,编译器可以帮助阻止它。

C makes guesses about the return type of a function when you use it before you've told C enough about the function -- it's name, return type, const-ness, and arguments. If those guesses are wrong, you get the error. In this case, they ARE wrong. Use a prototype or move the function above the call.

Oh, and about CONST-ness:
this means that the value of a function will be the same if you call it again with the same parameters, and that there should be no (important) side effects. This is useful for optimization, and also it makes a documentary claim that the compiler can enforce concerning parameters. A function promises not to alter a constant, and the compiler can help prevent it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文