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

Data types

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



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

Data types
PostPosted: Mon Aug 29, 2022 5:10 am     Reply with quote

Hello everyone
ccs c version 5.093

My variables are defined in the global field.
I have two ADC values. When I printf by taking the difference of these, the results show -67 or 409044448 values on the screen. What could be the reason for this change?
Code:

float T;
int32   avg_CO  ,avg_IN ;
int32 resoult = 0;

   if ((T>= 20)  && (T <= 29.99)) {(int32)resoult = (avg_CO-avg_IN);}

printf("Resoult= %ld\n\r  ",resoult);

AVG_IN= 231 AVG_CO= 164 Resoult= -67
AVG_IN= 231 AVG_CO= 164 Resoult= 409044448
Ttelmah



Joined: 11 Mar 2010
Posts: 19554

View user's profile Send private message

PostPosted: Mon Aug 29, 2022 8:18 am     Reply with quote

You have your int32 variables declared as _unsigned_ types. You need
to use signed int32.
You are printing an unsigned variable as a signed result. Also when
you subtract, if the result is negative, the maths overflows. Outcome
'disaster'....
Because the variables are unsigned, unsigned maths is used.
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

PostPosted: Mon Aug 29, 2022 8:51 am     Reply with quote

Ttelmah wrote:
You have your int32 variables declared as _unsigned_ types. You need
to use signed int32.
You are printing an unsigned variable as a signed result. Also when
you subtract, if the result is negative, the maths overflows. Outcome
'disaster'....
Because the variables are unsigned, unsigned maths is used.


I'm asking with apologies.
I am using variables as signed int32(int32).
you mean int32 resoult=0; should I not write?
newguy



Joined: 24 Jun 2004
Posts: 1911

View user's profile Send private message

PostPosted: Mon Aug 29, 2022 9:33 am     Reply with quote

The issue is the CCS compiler (for all versions except for PCD [16 bit compiler]) treats all variables as unsigned by default. Unless you explicitly say "signed" in front of your int32 declaration, the compiler treats that variable as unsigned.

With PCD, the compiler treats all variables as signed by default.
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

PostPosted: Wed Aug 31, 2022 3:07 am     Reply with quote

newguy wrote:
The issue is the CCS compiler (for all versions except for PCD [16 bit compiler]) treats all variables as unsigned by default. Unless you explicitly say "signed" in front of your int32 declaration, the compiler treats that variable as unsigned.

With PCD, the compiler treats all variables as signed by default.


Thank you very much for the information. After making the relevant correction, I receive such a warning. Should I take it into account?

>>> Warning 216 Line 489(2,3): Interrupts disabled during call to prevent re-entrancy: (@DIVS3232)
Ttelmah



Joined: 11 Mar 2010
Posts: 19554

View user's profile Send private message

PostPosted: Wed Aug 31, 2022 5:50 am     Reply with quote

It means you are using signed 32bit integer arithmetic somewhere in
one of your interrupt handlers.
Ask yourself the question 'do I really need to'?.
Integer arithmetic is pretty fast, but division is the slowest.
Generally it is good practice to minimise the complexity of everything
done inside such handlers.
Most operations inside handlers can be better done with smaller and
faster operations.
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