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.
Adding the prototype of method() before you call it will fix the error.
This error tells us that
method()was created by the compiler (because it didn’t find it) with a different return type thanconst int(probably int).This other error tells us that in fact the compiler created its own version of
method.