开发者

Const return types in C

开发者 https://www.devze.com 2023-01-12 09:25 出处:网络
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 c

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.


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!


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消