Searching \ for '[PIC] C18 compiler issue with casting' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'C18 compiler issue with casting'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] C18 compiler issue with casting'
2006\09\18@122846 by Kenneth Lumia

picon face
Hi,

Okay, I'm ready to tear out what's left of my hair!

I am either making a really stupid error, or there
is a bug in the C18 compiler (PIC18F4620).  I'm attempting
to cast a variable from 8 bits to 32 bits and it appears that the
sign is not being properly extended.  Just to really annoy
me, I tried it in VC++ and it works correctly.  I've
tracked the "bug" down to the following simplified code:

typedef char INT_8;
typedef long INT_32;

void main()
{

   INT_32 offset;
   INT_8 temp;    


   temp = -5;
   
   offset = (INT_32)temp;     //VC++ correctly reports 0xffff fffb
                                              //C18 reports 0x0000 00fb

}

Is it me or the compiler?

Thanks,

Ken
________________________________________
"Well that never happened in any of the simulations"
spam_OUTklumiaTakeThisOuTspamadelphia.net

2006\09\18@125327 by William Couture

face picon face
On 9/18/06, Kenneth Lumia <.....klumiaKILLspamspam@spam@adelphia.net> wrote:
> Hi,
>
> Okay, I'm ready to tear out what's left of my hair!
>
> I am either making a really stupid error, or there
> is a bug in the C18 compiler (PIC18F4620).  I'm attempting
> to cast a variable from 8 bits to 32 bits and it appears that the
> sign is not being properly extended.  Just to really annoy
> me, I tried it in VC++ and it works correctly.  I've
> tracked the "bug" down to the following simplified code:
>
> typedef char INT_8;
  ^^^^^^^^^^^^^^^^^^^^^^

I'm not familiar with C18, but is a "char" a signed 8-bit quantity or an
unsigned 8-bit quantity?  It is is signed, you would expect the VC++
behavior.  If it is unsigned, you would get the C18 behavior.

Try
  typedef signed char INT_8;
and see if that fixes your problem.

Bill

--
Psst...  Hey, you... Buddy...  Want a kitten?  straycatblues.petfinder.org

2006\09\18@130958 by John Temples

flavicon
face
If you expect "char" to be signed, use "signed char".  C does not
specify whether plain "char" is signed or unsigned.

On Mon, 18 Sep 2006, Kenneth Lumia wrote:

{Quote hidden}

> --

2006\09\18@131719 by Michael Rigby-Jones

picon face


>-----Original Message-----
>From: .....piclist-bouncesKILLspamspam.....mit.edu [EraseMEpiclist-bouncesspam_OUTspamTakeThisOuTmit.edu]
>Sent: 18 September 2006 18:10
>To: Microcontroller discussion list - Public.
>Subject: Re: [PIC] C18 compiler issue with casting
>
>
>If you expect "char" to be signed, use "signed char".  C does
>not specify whether plain "char" is signed or unsigned.
>

I think it's configurable via the compiler command line.  I always use the exact width integer types defined in <stdint.h> to prevent this kind of problem.

Regards

Mike

=======================================================================
This e-mail is intended for the person it is addressed to only. The
information contained in it may be confidential and/or protected by
law. If you are not the intended recipient of this message, you must
not make any use of this information, or copy or show it to any
person. Please contact us immediately to tell us that you have
received this e-mail, and return the original to us. Any use,
forwarding, printing or copying of this message is strictly prohibited.
No part of this message can be considered a request for goods or
services.
=======================================================================

2006\09\18@131928 by Kenneth Lumia

picon face
Bill,

Thanks.  That was the problem.  Excuse me while I hide
under my desk for the rest of the afternoon.
How embarrassing.

Ken.

_______________________________________
"Well that never happened in any of the simulations"
klumiaspamspam_OUTadelphia.net

{Quote hidden}

> --

2006\09\18@134020 by Wouter van Ooijen

face picon face
> I'm not familiar with C18, but is a "char" a signed 8-bit
> quantity or an
> unsigned 8-bit quantity?  It is is signed, you would expect the VC++
> behavior.  If it is unsigned, you would get the C18 behavior.

In ANSI C it is left to the compiler to decide whether a char is signed
or unsigned. So if you want to be sure of anything outside 0..127 you
will have to declare a 'signed char' or 'unsigned char'.

Wouter van Ooijen

-- -------------------------------------------
Van Ooijen Technische Informatica: http://www.voti.nl
consultancy, development, PICmicro products
docent Hogeschool van Utrecht: http://www.voti.nl/hvu


2006\09\18@134212 by William Couture

face picon face
On 9/18/06, Kenneth Lumia <@spam@klumiaKILLspamspamadelphia.net> wrote:

> Thanks.  That was the problem.  Excuse me while I hide
> under my desk for the rest of the afternoon.
> How embarrassing.

If that's the worst thing that ever happens to you, you're
better than the rest of us.

Bill

--
Psst...  Hey, you... Buddy...  Want a kitten?  straycatblues.petfinder.org

2006\09\18@134557 by Kenneth Lumia

picon face
Yep,  the manual states that a 'char' defaults to signed, unless
you add a -k to the command line (or have the "treat 'char'
as unsigned" box ticked like I did).  Of course "signed" should be
used in the typedef for portability.

Ken
________________________________________
"Well that never happened in any of the simulations"
KILLspamklumiaKILLspamspamadelphia.net

{Original Message removed}

More... (looser matching)
- Last day of these posts
- In 2006 , 2007 only
- Today
- New search...