inline function

Jun 3, 2011 at 6:54am
I try to use next declaration of inline function:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int main ()
{
    ...
    test();
    ....
    return 0;
}

inline void test(void)
{
    puts("Hello!");
}


And compiler shows me next error:
expected =, , , ; , asm or __attribute__ before void

What is wrong?
Jun 3, 2011 at 8:21am
Already got it.
I checked out mode: "In C mode, support all ISO C90 programs."

Jun 3, 2011 at 12:20pm
The inline function declaration needs to appear before it's used. Mode, whatever that is, has nothing to do with it.
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

inline void test(void)
{
    puts("Hello!");
}

int main ()
{
    test();
    return 0;
}
Jun 29, 2011 at 6:49am
btw, should we use the inline keyword? what's that used for compared if we don't use it? (for me, it is the same)
Jun 29, 2011 at 8:23am
It basically tells the compiler that instead of calling a function (which could involve putting variables onto the stack, allocating memory, stuff that takes extra time), it could try to just insert the code directly at the call site, like this:

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

inline void test(void)
{
    puts("Hello!");
}

int main ()
{
    test();
    return 0;
}


becomes

1
2
3
4
5
6
7
#include <stdio.h>

int main ()
{
    puts("Hello!");
    return 0;
}
Jun 29, 2011 at 9:01am
i just wanna get it straight so i don't have misunderstanding, when you call a function within main() the calling got stacked in the memory, like the value that passed by value or so forth, rather of calling it inlinedly which is faster... am i right?
Jun 29, 2011 at 10:26am
Pretty much.
Topic archived. No new replies allowed.