ThexXTURBOXx
ThexXTURBOXx

Reputation: 391

Are nested inline functions allowed (by the standard) in C programming language?

I just learned that inline functions can be defined in another function's body as well.

I'm using the mpicc compiler and the following code compiles successfully without warnings and errors:

#include <stdio.h>

int main() {
    inline int inlinetest(int x) {
        return x * 4;
    }

    printf("%d\n", inlinetest(8));
    return 0;
}

However, CLion shows the following errors within the file:

A the { bracket of inlinetest: Function definition is not allowed here

Where I try to call inlinetest: Implicit declaration of function 'inlinetest' is invalid in C99

I tried setting the CMAKE_C_STANDARD and C_STANDARD to 11, but this doesn't make a difference.

Is there a way to configure CMake or CLion itself, such that it doesn't detect this "error"?

Upvotes: 3

Views: 1498

Answers (3)

Mike Kinghan
Mike Kinghan

Reputation: 61630

As I'm sure you're aware, your mpicc compiler is a wrapper for a real compiler, which is your case apparently is gcc.

In your example you have defined inlinetest as a nested function within main.

Nested functions are a language extension in GNU C They are not supported by Standard C.

gcc will compile your program clean with default options:

$ cat main.c
#include <stdio.h>

int main() {
    inline int inlinetest(int x) {
        return x * 4;
    }

    printf("%d\n", inlinetest(8));
    return 0;
}
$ gcc main.c
$ ./a.out
32

but gcc will complain if you direct it to enforce any C standard from the oldest:

$ gcc -std=c89 -pedantic main.c
main.c: In function ‘main’:
main.c:4:5: error: ‘inline’ undeclared (first use in this function)
    4 |     inline int inlinetest(int x) {
      |     ^~~~~~
main.c:4:5: note: each undeclared identifier is reported only once for each function it appears in
main.c:4:11: error: expected ‘;’ before ‘int’
    4 |     inline int inlinetest(int x) {
      |           ^~~~
      |           ;

to the latest:

$ gcc -std=c18 -pedantic main.c
main.c: In function ‘main’:
main.c:4:5: warning: ISO C forbids nested functions [-Wpedantic]
    4 |     inline int inlinetest(int x) {
      |     ^~~~~~

I'm not familiar with CLion, but IDEs that boast on-the-fly code analysis always harbour the potential nuisance that the on-the-fly analyser will disagree with the compiler on some points. The IDE will probably give you some way to review the analyser's rules catalog and tweak it to silence nuisance diagnostics. But I would not hope you can get CLion to recognize GNU C nested functions: they are arcane.

And for that reason I suggest that you don't pick this fight. You surely haven't found a compelling reason to make you code rely on support for GNU C nested functions, so just don't bother with them.

Upvotes: 2

chqrlie
chqrlie

Reputation: 145307

The C Standard does not support nested function definitions. Some compilers allow them as an extension, notably gcc. Note however that since your nested function does not refer to local variables in the outer function, you can just move its definition outside the body of the outer function:

#include <stdio.h>

inline int inlinetest(int x) {
    return x * 4;
}

int main() {
    printf("%d\n", inlinetest(8));
    return 0;
}

Note also that inline may be ignored by the compiler, and that some modern compilers will inline functions based on actual usage even without the inline keyword.

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 224546

The C standard allows nested function definitions in the sense that it allows C implementations to support such definitions as an extension to the core language: a conforming program may have nested function definitions but can only be used with compilers that support them.

The C language does not allow nested function definitions in the sense that they are defined as part of the core language: a strictly conforming program does not have nested function definitions.

With a compiler not designed to support nested function definitions, you cannot expect a switch can be easily provided to “ignore” the errors resulting from nested function definitions. Supporting nested function definitions requires deliberate design and engineering in the compiler.

Upvotes: 1

Related Questions