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 CCS Technical Support

Global variable unchanged in main()

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



Joined: 20 Oct 2011
Posts: 15

View user's profile Send private message

Global variable unchanged in main()
PostPosted: Tue Feb 09, 2021 10:00 am     Reply with quote

I am running PCH 5.101, Linux version.

A variable is declared in an include file before main() and hence a global variable.
When modifying the variable in a function being in the same .c file, it does not have a changed value in the main function after return from the function.
Even when adding an 'extern' the same happens.
I expected that the variable has the same value in main(), or more general, a variable has the same value in main() and functions.
temtronic



Joined: 01 Jul 2010
Posts: 9241
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 09, 2021 10:46 am     Reply with quote

You should post a small program that this happens to as 'something' isn't right.
Off chance do you have #CASE in your program ??
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Tue Feb 09, 2021 10:52 am     Reply with quote

Being in an include file doesn't automatically make it global. The include file
might have a function definition around the variable, in which case
it'd be local to that function.
Look at the symbol table (Symbols from the compile menu in the IDE).
If it is global, the symbol name will be shown as an entry with nothing
else in front of it except the memory address. If instead you have the name
as something like xxxx.variable, then it is declared inside a function not
globally.
The same search would show the other possibility that there is actually a
second variable declared inside something else.
What is the variable name?.
berel



Joined: 20 Oct 2011
Posts: 15

View user's profile Send private message

PostPosted: Wed Feb 10, 2021 10:19 am     Reply with quote

Thanks for both hints.
#CASE was not used, the variable name is currentPosition which should be unique.
I wrote a small sample program with global variables (output pulses as often as the global variable value) to share it, and that worked as expected.

I then found in the previous project following Ttelmah's hint that the global variable was declared in an include file and this one was included in the main.c and another source file, hence being different variables with same name.
I know, no good idea to have a global variable in an include file, had some historic reasons and is usually not done.
Confusing thing that the project ran fine with the XC8. XC8 seems to handle 'multiple' global variables in a different way.
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Wed Feb 10, 2021 11:00 am     Reply with quote

Glad you found it.
The symbol table really is your 'friend' in finding this type of problem. Smile
berel



Joined: 20 Oct 2011
Posts: 15

View user's profile Send private message

PostPosted: Wed Feb 10, 2021 11:04 am     Reply with quote

I will do so Very Happy
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