Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Const return types in C

Tags:

c

constants

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.

like image 567
Leif Andersen Avatar asked Aug 22 '10 14:08

Leif Andersen


People also ask

What is a const return type?

(C++) const return typeThe value of a return type that is declared const cannot be changed. This is especially useful when giving a reference to a class's internals (see example #0), but can also prevent rarer errors (see example #1). Use const whenever possible [1-7].

Can you return const in C?

The language specifies that no const shall be specified on a function type. The language totally allows it on return types. That page's example applies it to a function types (i.e to the type of a function, not to the type of a return type like the text claims at the end).

What is const type in C?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.

How many return types are there in C?

Similarly, a function can return something, otherwise does not return anything. So we can categorize them into four types.


2 Answers

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!

like image 143
CB Bailey Avatar answered Oct 04 '22 06:10

CB Bailey


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.

like image 37
Luca Matteis Avatar answered Oct 04 '22 06:10

Luca Matteis