#include <stdio.h>
static int  Var1;
static FUNC()
{
    //do stuff with var
}

void CALL_FUNC()
{
    //call FUNC
        //do stuff
}

void fc1(void)
{
    CALL_FUNC();
}

void fc2(void)
{
    CALL_FUNC();
}

void fc3(void)
{
    CALL_FUNC();
}

//etc.

void main()
{
}

goal is to break the fc1, fc2 and others into different files and at the same time keep the static-ness of the var1 and FUNC().
need ideas how to do this best to avoid violation of C rules?. creating a .c and put the var1, func() and call_func() in it and call the call_func() from other files is the best way I know so far.

Recommended Answers

All 5 Replies

What you're asking is nonsensical. You want var1 and FUNC to have file scope, yet still be visible in other files. Why?

My guess is that you're confusing how static is overloaded and want semantics that are actually already there with extern functions and variables. It might help to explain exactly what you want to accomplish here, rather than how you think it should be done.

I was thinking doing something like, is this possible?.:

call.c:

#include <stdio.h>
static int Var1;

static FUNC()
{
//do stuff with var
}

void CALL_FUNC()
{
    //call FUNC
    //do stuff
}

fc1.c:

void fc1(void)
{
    CALL_FUNC();
}

fc2.c:

void fc2(void)
{
    CALL_FUNC();
}

//etc.
main.c:

void main()
{
}

First off, you should never declare main() as void in C or C++; in C, int main() is the only portable form of the function, and in C++ (and the most recent C standard) it is actually required to return int.

Now, with that nitpick out of the way, I have to say that what you're doing doesn't make much sense. Let's break it down piece by piece:

You declare FUNC() to have file scope, and than put a wrapper around it (CALL_FUNC()) with top-level scope. Why? WHat is the wrapper protecting?

You then call CALL_FUNC() in two different source files, but without any function prototypes to make the function definition visible to the calling functions.

Finally, you have an empty main(). What is that intended to accomplish?

Can you explain, in words (not code) what your intention is? Is there a reason for Var1 and FUNC() to be declared static, and if so, what is it?

Finally, I would recommend reading this post for further Illumination fnord on the subject of header files, function prototypes, and so forth.

let's think of var1 as a lock like a mutex lock that different functions preferably in different files can access it, something like in above. you can think of FUNC() as something like a _lock() that uses the static var1. That is what I am trying to do. The actual code has not been implemented yet; It is still in design stage. If there is a sample code that does what I am after, I like to see it.

How different functions in different files (.c) can call block() and unblock()?.

static Mutex mutex;

static block () {
  mutex.lock();
}

static unblock () {
  mutex.unlock();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.