CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

local & global variables

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
planet69



Joined: 25 Aug 2010
Posts: 8

View user's profile Send private message

local & global variables
PostPosted: Mon Nov 08, 2010 9:27 pm     Reply with quote

Why do we need to have separate local & global variables.

I've completed several programs without ever declaring any local variables and everything as globals.
I avoid any 'conflict' that may come my way with globals.

What is the advantage of using local variables anyway other than that they may be re-used by the compiler to save limited RAM space.

Just a thought.
Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 08, 2010 10:00 pm     Reply with quote

For small programs it's not so bad. When you start to do projects that
contain many modules (files), there are too many variables to keep
track of mentally, and you will see the need for using mostly local
variables. See this page for more reasons:
http://c2.com/cgi/wiki?GlobalVariablesAreBad
pmuldoon



Joined: 26 Sep 2003
Posts: 218
Location: Northern Indiana

View user's profile Send private message

PostPosted: Tue Nov 09, 2010 7:47 am     Reply with quote

In very small programs it doesn't matter much. But I prefer to use locals and static locals whenever possible because I know the scope of them (only used within that function) and they are easier to keep track of.
And they can also be given very specific and descriptive names.

Globals can create problems when you start to lose track of which functions are using them / changing them, and what their purpose is.

I would suggest being as descriptive as possible with your variable names, and limit the scope of them whenever possible. When you come back to your program 1 or 2 years from now to add a new feature, it will be that much easier to see that you're not creating some unintended side effect with the change.

Just my thoughts...
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Nov 09, 2010 8:01 am     Reply with quote

I write everything as small drivers, each driver file contains its own global variables. I keep track what they are for that way. If they are on the driver file, they only affect the driver functions....basically small groups of globals...works for me...
_________________
CCS PCM 5.078 & CCS PCH 5.093
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Tue Nov 09, 2010 9:54 am     Reply with quote

The other 'key' thing is memory usage.

If you have a subroutine, that uses 20 bytes for a character array, and some other variables. If these are 'global', this memory is being used by the variables all the time. If you declare the variables as local, the same memory area, can be re-used by other routines.
Only using global, suggests 'small code'. With larger code, and more memory in use, the ability to re-use parts of the memory becomes essential....

Best Wsihes
planet69



Joined: 25 Aug 2010
Posts: 8

View user's profile Send private message

PostPosted: Tue Nov 09, 2010 9:58 pm     Reply with quote

Appreciate the feedback. enlightening.

Anyway, I've done some pretty large programs but always with some RAM to spare.
I guess my 'large programs' are not that 'large' after all.
Laughing

Thank you.
pmuldoon



Joined: 26 Sep 2003
Posts: 218
Location: Northern Indiana

View user's profile Send private message

PostPosted: Wed Nov 10, 2010 6:10 am     Reply with quote

If it works for you, then it works. Chances are you are like me and nobody else will probably ever read your programs.

But keep in mind, as your skills grow, so will the complexity of your programs.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group