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

keyword "static"

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







keyword "static"
PostPosted: Tue Jun 01, 2004 1:45 pm     Reply with quote

According to the manual, the keyword "static" is
Code:

Variable is globally active and initialized to 0


The brief description leads me to conclude that, even if i use static inside a function, then it is globally active, and thus, I shouldn't use the same name (in this case, "initialized") more than once.

However I have used "static" several times INSIDE a function, without problems.

e.g.

Code:

int fxn1(void)
{
    static int initialized;
    ....
}

int fxn2(void
{
    static int initialized;
    ....
}
...


It seems to work just fine, contrary to how I interpret the description in the CCS manual. Each instance of "initialized" retains its value even after exiting and re-entering the functions. The CCS manual leads me to interpret that "initialized" is global, and thus cannot be used again.
Even though it seems to work fine, is this proper usage of static in the CCS environment Question

Furthermore, I really see no use for using "static" outside a function, since we cannot have multiple .c files compiling separately anyways.

-Mike
[/code]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 01, 2004 1:59 pm     Reply with quote

Here is the MS explanation of static:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/decla_7.asp
Ttelmah
Guest







Re: keyword "static"
PostPosted: Tue Jun 01, 2004 2:53 pm     Reply with quote

valemike wrote:
According to the manual, the keyword "static" is
Code:

Variable is globally active and initialized to 0


The brief description leads me to conclude that, even if i use static inside a function, then it is globally active, and thus, I shouldn't use the same name (in this case, "initialized") more than once.

However I have used "static" several times INSIDE a function, without problems.

e.g.

Code:

int fxn1(void)
{
    static int initialized;
    ....
}

int fxn2(void
{
    static int initialized;
    ....
}
...


It seems to work just fine, contrary to how I interpret the description in the CCS manual. Each instance of "initialized" retains its value even after exiting and re-entering the functions. The CCS manual leads me to interpret that "initialized" is global, and thus cannot be used again.
Even though it seems to work fine, is this proper usage of static in the CCS environment Question

Furthermore, I really see no use for using "static" outside a function, since we cannot have multiple .c files compiling separately anyways.

-Mike
[/code]

A variable declared inside a function, gets the name of the function appended to the front of it's declaration. So if you define a 'fred' inside 'main', and then another inside a subroutine, the two variables still have different names, and will not refer to the same storage area.
Have a look at the symbol declaration file to see how this is done.
Static used outside the functions, is a way of declaring a global variable, that is initialised to zero, without having to declare as:
int8 fred=0;
or use the 'zero RAM' declaration. The disadvantage of the latter, is that on larger chips, it takes a long time, given the amount of memory that can be involved.
Otherwise in the CCS C, it offers no real advantage.

Best Wishes
Devin Electronics
Guest







Global Variable VS Global Lifetime within subroutine
PostPosted: Thu Sep 15, 2005 1:07 pm     Reply with quote

Thanks for the link to the MS explanation. Now it makes since why my code doesn't work the way I thought it would.

STATIC: A variable declared at the internal level with the static storage-class specifier has a global lifetimebut is visible only within the block in which it is declared.

But maybe someone has a way to work around the problem. Trying to NOT declare my "message" variable in the main C code. For critical timing reason, I cannot rotate "message" within the transmit_message routine.

Psudocode as follows:

void transmit_1(void) {
subroutine to toggle two pins with a one frequency
rotate_left(&message+0,1);
//bit-bang some pins
rotate_left(&message+1,1);
//bit-bang some pins
rotate_left(&message+2,1);
//bit-bang some pins
rotate_left(&message+3,1); //Rotate MSB into CARRY
//bit-bang some pins
}

void transmit_0(void){
subroutine to toggle two pins at a different frequency
rotate_left(&message+0,1);
//bit-bang some pins
rotate_left(&message+1,1);
//bit-bang some pins
rotate_left(&message+2,1);
//bit-bang some pins
rotate_left(&message+3,1); //Rotate MSB into CARRY
//bit-bang some pins}

void transmit_message (int32 msg) {
int8 count;
static32 int32 message;

for (count=0; count<=31;count++) { //loop through message
if (STATUS.0 == 0)
transmit_1();
else
transmit_0();
}
}
Guest








PostPosted: Thu Sep 15, 2005 1:19 pm     Reply with quote

Never mind. Found the solution. Code mentioned was part of an #include file. Found another post that mentioned just putting the "int32 message" declaration at the top line of the #include file outside of any routines. Works.
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