Global variable

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Global variable

983 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by xOrElse on Thu Jul 19 10:30:00 MST 2012
Hello,
I have problem with global variable.
Where to put global variable and how to declare it?

In main.c document I put volatile int globala=0; and it's not recognized as global. When I'm in functions I cant use it, the debugger reports it as globala isn't defined. I have no idea what to do. I'm new, and this program is so confusing.

Please help me

P.S. I used to program in C (in school), so basically I understand syntax. Main problem is where in lpcxpresso should I put global variable.
0 Kudos
7 Replies

865 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by xOrElse on Fri Jul 20 04:21:26 MST 2012
thank you!
0 Kudos

865 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ex-kayoda on Fri Jul 20 04:15:13 MST 2012
Googling for "C tutorial" will show you about 601 million results (here) :eek:

Perhaps http://www.cprogramming.com/tutorial/c-tutorial.html ?
0 Kudos

865 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by elef on Fri Jul 20 04:08:15 MST 2012

Quote: xOrElse

Do you know maybe where (which site) could I learn syntax better? As you can see my knowledge isn't so impressive, I know something, but not very much.



There are thousands. Just google C syntax and go through a few websites till you find one you like.
0 Kudos

865 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by xOrElse on Fri Jul 20 04:04:13 MST 2012
THANK YOU SOOOO MUCH!!!!

Do you know maybe where (which site) could I learn syntax better? As you can see my knowledge isn't so impressive, I know something, but not very much.
0 Kudos

865 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ex-kayoda on Thu Jul 19 11:14:51 MST 2012

Quote: xOrElse
It still doesn't work...



Because that's nonsense

#1 Define a global in gpio.c
volatile int crveni=0;
volatile int plavi=0;
#2 Add externs in gpio.h
extern volatile int crveni;
extern volatile int plavi;
Now all files which include gpio.h know that there are your globals crveni and plavi :):)
0 Kudos

865 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by xOrElse on Thu Jul 19 10:56:48 MST 2012
It still doesn't work, I don't know what to do!

Here is my code, could you please tell me where to put extern variable?
0 Kudos

865 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ex-kayoda on Thu Jul 19 10:46:16 MST 2012

Quote: xOrElse
I used to program in C (in school), so basically I understand syntax.



I don't think so :rolleyes:

http://en.wikipedia.org/wiki/Global_variable


Quote:

The C language does [COLOR=Red]not have a global keyword[/COLOR]. However, variables declared outside a function implicitly have a scope covering everything in the .c file or compilation unit containing its declaration. In a small program contained in a single file, such variables effectively have global scope. On the other hand, a variable that is required to have global-scope in a[COLOR=DarkOrange] multi-file project needs to be imported individually into each file using the extern keyword[/COLOR].

So don't forget to use
extern volatile int myglobal;
in multi-file project if your global is defined in another file of your project :eek:

That's C, not LPCXpresso
0 Kudos