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

Casting of unsigned variables for function call

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



Joined: 21 Nov 2006
Posts: 129

View user's profile Send private message

Casting of unsigned variables for function call
PostPosted: Sat Mar 21, 2009 9:43 am     Reply with quote

Hi,

Quick, hopefully easy, question...

Say there is the following function prototype:

Code:
void myfunc(signed long error_value);


If I have the following variables:

Code:
unsigned long command, actual;


... and want to pass into the function this:

Code:
myfunc(command - actual);


... do I need to cast it like this:

Code:
myfunc((signed long)(command - actual));


or will it work fine without the cast? I could do a little experiment to figure it out, but thought if someone else had the question in the future, it would be good to show up in the forum.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Mar 21, 2009 10:11 am     Reply with quote

I guess, that CCS C will accept it without a type cast and (in this case) also shows identical results. Cause some compilers will cause an error and many at least a warning, I prefer a type cast.
Ttelmah
Guest







PostPosted: Sat Mar 21, 2009 10:25 am     Reply with quote

C, should automatically cast on any compiler in this situation. It is explicitly stated in the original K&R books, that it should do so.

_However_, I'd cast first, but in a different way:
Code:

myfunc((signed long)command - actual);

Note the removal of the brackets.
This converts the value to signed, _before_ the arithmetic. The arithmetic itself, is then done using 'signed', which ensures the correct result, if actual is larger than command.

Best Wishes
evsource



Joined: 21 Nov 2006
Posts: 129

View user's profile Send private message

PostPosted: Sat Mar 21, 2009 11:27 am     Reply with quote

Ttelmah wrote:

_However_, I'd cast first, but in a different way:
Code:

myfunc((signed long)command - actual);


Best Wishes


Wouldn't that just cast "command" as a signed long? Wouldn't you have to do this?:

Code:

myfunc((signed long)command - (signed long)actual);
Guest








when in doubt -compile and see
PostPosted: Sat Mar 21, 2009 12:00 pm     Reply with quote

when i'm not sure - i compile both ways - and compare the .LST files to see what happens with each variant approach

more educational that way

but for my $$ - i'd bet on Ttelmah frankly
evsource



Joined: 21 Nov 2006
Posts: 129

View user's profile Send private message

Re: when in doubt -compile and see
PostPosted: Sat Mar 21, 2009 1:41 pm     Reply with quote

Anonymous wrote:


but for my $$ - i'd bet on Ttelmah frankly


LOL, I would bet on Ttelmah too - I wasn't trying to say that I thought I was right. I just would have thought it was that way intuitively, and want to know why (or if) my intuition is wrong! Very Happy
Ttelmah
Guest







PostPosted: Sat Mar 21, 2009 1:43 pm     Reply with quote

Key is again in K&R.
Types have a 'precedence'. So a float (for example), is seen as a 'higher' type than an int. In this order, signed variables, are seen as a higher type, than unsigned. Now when performing arithmetic, C uses the type of the 'higher' variable. So converting just one of the variables to 'signed', forces signed arithmetic to be used.

Best Wishes
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

PostPosted: Sat Mar 21, 2009 2:01 pm     Reply with quote

Ttelmah wrote:
Key is again in K&R.
Types have a 'precedence'. So a float (for example), is seen as a 'higher' type than an int. In this order, signed variables, are seen as a higher type, than unsigned. Now when performing arithmetic, C uses the type of the 'higher' variable. So converting just one of the variables to 'signed', forces signed arithmetic to be used.

Best Wishes

First of all, you have it backwards. Combining signed and unsigned results in unsigned, according to K&R.

Secondly, in this case it does not matter if any casting is done at all. Unless you are checking for overflow, the code to subtract two signed longs is the same as the code to subtract two unsigned longs.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
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