Return types Const in C

I read some code examples and they returned const int. When I tried to compile the sample code, I got errors regarding conflicting return types. So I started looking, thinking that the problem with const was a problem (when I deleted it, the code worked fine, it not only compiled, but also worked as expected). But I could never find information specifically related to the type of constant return (I did for structures / parameters / etc. Etc., But I did not return types). So I tried writing a piece of code to just show what const can 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

, , const, , , 5, . , - , const, . .

+3
5

() .

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

, method.

+4

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

, !

+18

C , , C - , , . , . . .

, CONST-ness: , , () . , , . promises , .

+5

, , undefined: method, . . :

#include <stdio.h>

const int method() {
    return 5;
}

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

. , .

+4

main method() , , int. const int. method() main main.

+2

Source: https://habr.com/ru/post/1760993/


All Articles