I would like to define global variables they are shared.
to be sure I would like to ask how to do it correctly to prevent issues.
Let talk about two files: example:
application.c:
uint16_t temp
temp=5;
main.c
extern uint16_t temp
temp=10;
what is correct way to do it? maybe definition in header files, includes needs to be used? or. Is there some reference document explaining basic in C code, variables, definitions, pointers, symbols involved in simplicity studio, where I can reffer?
Thanks a lot
Valentin
Discussion Forums
Simplicity Studio
Answered
Answered
What you wrote is essentially correct in C. What you're asking for is 'what is good practice?' So I'll give you my opinion.
You must define the variable in only one place and in a .c file.
Declare the variable extern in an accompanying header file.
Include that header file when defining the variable, so the compiler can check for conflicts.
This way you let the compiler check your interface definition and you don't unnecessarily duplicate volatile code. When you need to change the name or type of the variable there are only two places to modify.
Maarten
P.S. Even though I set the numbering to start at 4 and 5 resp. the stupid forum resets it back to 1 each time.
Correct Answer
2
Thanks a lot Maarten, that helps, will follow your opinion
Global variable definition sharing across files
Hi
I would like to define global variables they are shared.
to be sure I would like to ask how to do it correctly to prevent issues.
Let talk about two files: example:
application.c:
uint16_t temp
temp=5;
main.c
extern uint16_t temp
temp=10;
what is correct way to do it? maybe definition in header files, includes needs to be used? or. Is there some reference document explaining basic in C code, variables, definitions, pointers, symbols involved in simplicity studio, where I can reffer?
Thanks a lot
Valentin
What you wrote is essentially correct in C. What you're asking for is 'what is good practice?' So I'll give you my opinion.
This way you let the compiler check your interface definition and you don't unnecessarily duplicate volatile code. When you need to change the name or type of the variable there are only two places to modify.
Maarten
P.S. Even though I set the numbering to start at 4 and 5 resp. the stupid forum resets it back to 1 each time.
Thanks a lot Maarten, that helps, will follow your opinion
Valentin