View previous topic :: View next topic |
Author |
Message |
evsource
Joined: 21 Nov 2006 Posts: 129
|
Casting of unsigned variables for function call |
Posted: Sat Mar 21, 2009 9:43 am |
|
|
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
|
|
Posted: Sat Mar 21, 2009 10:11 am |
|
|
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
|
|
Posted: Sat Mar 21, 2009 10:25 am |
|
|
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
|
|
Posted: Sat Mar 21, 2009 11:27 am |
|
|
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 |
Posted: Sat Mar 21, 2009 12:00 pm |
|
|
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
|
Re: when in doubt -compile and see |
Posted: Sat Mar 21, 2009 1:41 pm |
|
|
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! |
|
|
Ttelmah Guest
|
|
Posted: Sat Mar 21, 2009 1:43 pm |
|
|
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
|
|
Posted: Sat Mar 21, 2009 2:01 pm |
|
|
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 |
|
|
|