View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
byte? |
Posted: Tue Oct 23, 2012 12:32 am |
|
|
what is the difference between declaring:
int8 var_name
and
byte var_name |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Tue Oct 23, 2012 1:29 am |
|
|
Nothing.
Just other names.
In fact if you look in the include file for the processor, you will find the line:
#define BYTE int8
int = int8 = char = byte
on the PIC18/16.
Danger is though that if you switch to a PIC24, int is no longer in this list.
This is why in general it is 'better' practice to use:
intn sizes (like int8, int16, int32) for numeric values.
Then use 'char' for alphabetic text, and (possibly) use 'byte' for things that are being sent over 'byte wide' transport media (RS232 etc.).
This way you are confident about the sizes, and 'know' they are suitable for what is being done with them.....
Best Wishes |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Tue Oct 23, 2012 10:14 am |
|
|
Thx a lot but what happens with int at pic24? As I understand it is no longer int8. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Oct 23, 2012 11:47 am |
|
|
Sigh.... what is the part you didn't understand?
Yes, that is what Ttelmah wrote: Quote: | #define BYTE int8
int = int8 = char = byte
on the PIC18/16.
Danger is though that if you switch to a PIC24, int is no longer in this list. |
And that is the same reason why he wrote: Quote: | it is 'better' practice to use:
int sizes (like int8, int16, int32) for numeric values. |
So, when you write everywhere in your program int8 or int16 instead of just 'int', then then it doesn't matter when you change to another compiler where the size of 'int' is 8, 16 or 32 bits.
For the exact size and definitions of all the compiler defined types have a look in the PCD Reference Manual, chapter "Data definitions". |
|
|
Battery David
Joined: 01 Feb 2010 Posts: 25
|
|
Posted: Tue Oct 23, 2012 12:24 pm |
|
|
I was poked in the ribs by a "real" programmer once to get me to be explicit on every variable type. Since an 'int' could be different on different parts, I now always do full declarations:
Code: |
unsigned int8 Dog;
signed int16 Fish;
unsigned int1 ControlBit;
|
|
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Oct 23, 2012 12:36 pm |
|
|
Because of my ugly roots in ASM land - I second batterydavids reply.
I have ALWAYS explicitly declared my integer Vars as signed or unsigned
( except for int 1 ;-))
and it is a good way to keep your thoughts and your program clear. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
|
Posted: Tue Oct 23, 2012 2:54 pm |
|
|
It's also worth noting that for PIC24 at least, that set of equalities isn't always true. Some versions of the compiler in the late 120s or early 130's default char to signed and int8s to unsigned, which can snag you sometimes. I haven't checked to see if that is still the case |
|
|
|