Static Variables

If I want to define a variable inside of a function and want it to be available to all functions in the .c file, will static do it?

Would this work?

void FunctitonWhatever1(void)
{
static int bob=1;
/Note the static and the =1/
CallerFuction();
}

void CallerFunction(void)
{
int bill=0;
/Only need it in this function/
bill=2+bob;
}

No, not as you listed it. If you declare a variable inside a function (static or not), it is only available inside that function. The difference is that a static variable declared inside a function will retain its value between calls to that function. So it acts like a global variable but it can only be seen by that function.

If you need to access a variable from many functions you can either make it a global variable or pass it around as part of the function calls.

Declare it at the top of a file, then in other files put


extern int bob;

at the top.

So basically like this.

MyFunction.c

void declarations(void)
{
int bob=2;
}

void hi(void)
{
int bill=0;
extern int bob;
bill=bob+2
}

I need to define a variable in a function and make it available to everywhere in the rest of the file.

No, there is no need for the declarations function. Just declare the variable outside of all functions. This will make that variable a global variable so it will be available to all the functions in the file. Also the extern int declaration is not needed when the variable is a global variable.

Your code with the above modifications would look like this:


MyFunction.c

int bob=2;

void hi(void)
{
int bill=0;
bill=bob+2;
}


declare the variable(s) you want to use outside of a function (outside any brackets).
Then, move the extern line(s) into a .h file (user_routines.h)
The extern lines are optional, and are only needed if you want to use them in more than one file (user_routines.c and user_routines_fast.c for autonomous)

After that, you can read and write to them from any function:

user_routines.c

#include "user_routines.h"
...
...
/* Only declare and initialize global variables *once*.*/
int x=0;
int y=0;
void func1(void) {
    x=1;
}
void func2(void) {
    y=x;
}
void Default_Routine(void) {
    /* caution: do not declare the global variables (or variables with the same name) inside a function again. This may cause strange behaviour if done by accident, and will not produce an error or even a warning in most cases.*/
    // int x; <-- NO COMPILE ERROR! 
    pwm01=y+127;
    pwm02=x+127;
}

user_routines.h

...
...
/* The extern allows *global variables* declared in one file to be usable in any other file. */
extern int x;
extern int y;
void func1(void);
void func2(void);
void Default_Routine(void);

So, just to recap. I can use extern in the header and declare them at the TOP of userroutines.c. They will be defined once and available to all of userroutines.c as well as anywhere else.
I thought C was run-by-call only. Does C have the exception that it will run everything outside of functions once and then goto rbc only?
My original plan was to delaclare my variables withing userinitialization() to make sure they run get declared once. But if I can just do it outside of all functions and add some stuff to .h and have it work; it is as good as putting it in userinitialization()

C is not C++, variable declarations don’t “run”. They are dealt with by the compiler and the linker but not the runtime. The only thing that executes is the function “main” and anything that it directly invokes.

Lynn (D) - Team Voltage Software

Actually, you can not put explicit code outside of functions, but you are allowed to put initial values for all variables. These must be constants, and can only be either arrays like (1,45,3,21,4,5…} or integers like 2.
They can also include basic expressions like:

#define M_PI 3.14159265358979
int my_circumference=2*M_PI

This code is executed, and I think that’s what the “.cinit” seciton in your map file is for.

But, no actual code is allowed, and the order in which variables are initialized is also undefined. So you can and should only use this to initalize defaults. If you do not do this, then the variables will be uninitialized, meaning that they could start out with any value.

If you want code to run, then put all of that in your User_Initialization(). Beware that no pwms or inputs or control inputs can be read or written in the initialization function.