0

Is it possible to put the variable declarations in an external function? After reading from Wikipedia that:

an inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined.

I hypothesized that the following might work. It did not take long for the compiler to slap my fingers :(

inline void declaration(){
    int a;
}

int main(){
    declaration();
    a=2;
    return 0;
}
2
  • 2
    The keyword in that Wikipedia article is requested. inline does not require the compiler to do as you ask, it is only a suggestion. Thus, inline functions must all have non-inline semantics. Commented Mar 8, 2012 at 8:07
  • It's worth noting, with respect to inline, that gcc implements multiple different versions of inline expansion, depending on whether you request C99 semantics, or gnu89 semantics, etc. It's kind of a mess. Commented Mar 8, 2012 at 11:21

5 Answers 5

4

This may not be how it is done but if you want a basic idea of how you can think about what happens when you inline a function.

Imagine the compiler turning your code into something like this, then you see why it will not work.

int main(){
    {
        int a;
    }
    a=2;
    return 0;
}

The call to declaration() is replaced by the contents of the function including brackets, thus int a; is declared in an inner scope and is not visible in the main function.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, but of course, you also can't goto into an inline function.
2

No, this is not possible.

What is possible, is to use a preprocessor directive #define:

#define VARBLOCK  int a, b, c; char ca, cb, cc;

int main() 
{
    VARBLOCK;
    a = 2;
}

This would be a bad practice. Also these would still be variables only available in the scope of function where it were placed, without values being shared.

Comments

1

No - as far as I'm aware an inline function must behave semantically equivalent to a non-inline function; it doesn't affect what counts as legal code. It's just an optimization.

In particular, you could have a variable called a in both functions, but they'd be separate variables on the stack.

(Even if you could do this, I'd suggest it would be a very bad idea in terms of readability.)

Comments

0

inline functions are usually just a function containing no more than about 4 lines and you would want the compiler to do the optimization you where talking about since it would be faster to do what the function does, rather than adding extra code.

Inline expansion is used to eliminate the time overhead when a function is called. It is typically used for functions that execute frequently.

So there's nothing special with the inline function, rather than it might be handled differently by the compiler. They don't share their stack with any other function, which would be the only way for main to use a variable that is created in a different scope.

So my tip is; write your functions, and treat them as you usally should. Then when you are done, inline the short ones that you use a lot.

And if you really wanna create a variable in another function, allocate it on the heap in the function and return a pointer that you save and then set to 2 (your case). :) Just remember to free the memory!

Comments

0

You can do this, though:

#include <stdio.h>

int* GetMyIntAddress(void)
{
  static int blah = 0;
  return &blah;
}

int main(void)
{
  printf("%d\n", *GetMyIntAddress());
  *GetMyIntAddress() = 123;
  printf("%d\n", *GetMyIntAddress());
  return 0;
}

blah will be a global variable defined in the scope of the GetMyIntAddress() function.

If you add inline to the definition of GetMyIntAddress(), you are risking to get multiple independent instances of blah if the inline function is used in different modules (e.g. included from a shared header file).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.