Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Two questions for you "C" gurus

Status
Not open for further replies.

GrandAlf

Advanced Member level 2
Joined
Mar 9, 2002
Messages
520
Helped
47
Reputation
92
Reaction score
6
Trophy points
1,298
Location
UK
Activity points
4,730
As a relative newcomer to C, I have a couple of questions that maybe someone could enlignten me on. The first is when a variable is declared outside any functions, I have read that it should also be declared within any function that uses it. I have found that it works just fine without doing this, anyone know why this should be a requirement?. Also when using an RTOS, and assuming I have a function within a task that is not using external variables, can I call it it as a normal function, or are there any timing problems in passing data backwards and forwards.

Any opinions gratefully received.
 

when a variable is declared out of a function it is a global variable and can be called or passed to any function when a variable is declared inside a function it is local to that function alone.
u don't have to re declare the gloval function inside any other fn as when u pass a variable it uses it.

correct me if iam wrong
 

I agree with you EcraZ, they work fine like this, its just that in the Ansi C book, it says external variable should also be declared in funcions that use them?. Beats me!
 

external variables are declared in one file but used in another file the compiler needs to know that the variable is declared in another file and so extern is used

eg
file1.c

extern int a;// a used in this file but declared someother file
int b;

function body;

file2.c

extern int b;// b used in this file declared other file
int a;

function body;
 

Ahh, I understand now. We live and learn.
Thanks
EcraZ
 

Be careful when declaring local and global variables using the same name because when modifying a variable inside a function with a local variable changes only the value of the local variable and not the one of the global.
For further information, please check in some C book the chapter that covers the scope of variables.
Hope it helped,
Maddin
 

Actually there are 3 types of declaration (If i am not worng those are colled storage specifiers in C ):
global - defined in file without static keyword . It is possible to reference it from other files . Memory allocation at compile time .
static - defined with static keyword . Only function in file where it is defined can refer to it . Memory allocation is done at compile time
temporary . Defined within function . Only function where this var is defined can refer to this variable .
Allocation is done during function cacll and ususally in stack memory place .

If you wqould like to define var outside the function you should ensure following :
a. if there can be 2 function calls at the same time
(interrupt or RTOS cases ) , there is possiblity that there will be interference and one function call can change its value while another function call was not expecting to have this . In that case the fucntion is called nonreentrant . To be able to make function reentrant (more than one call for the same fucntion at the same time ) you must :
1. define the vars as temporary (stack allocated )
2. If you use static or global variables , protect access to these vars via disabling interrupts when accessing those and enabling after all job with those has been done .
 

Thanks guys, I seem to have misunderstood the difference between Static and Global. Also thanks for pointing out possible RTOS probs. Much appreciated.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top