What's up with the integer size?

I have an array, I wanted to find how many elements I have in the array. I consulted google and found out that you can do:


sizeof(arrayname)

this returns the size in bytes of the array. If you want to find out how many elements you divide sizeof(arrayname) by the size of the data type your array contains. So I have an array of integers, I checked the ROBOTC’s help file on data types and integers are 2 bytes long.
so I thought that this:


int MyArray[12] = {1,2,3,4,5,6,7,8,9,10,11,12};
int NumOfArrayElements = sizeof(MyArray)/2;
displayLCDNumber(0, 0, NumOfArrayElements);

would display 12, but it displayed 24. So I have to divide by 4 like it’s a long.
Any explanation as to why?

ints are 4 bytes each, unsigned ints are 1, i think

Int is 4 bytes just about everywhere on Earth, the wiki is wrong.

Char 1 byte
Short 2 bytes
Int 4 bytes
Long 8 bytes

Unsigned aren’t any smaller instead they use the half of their range that would usually be used for negative numbers for positive.

IE int’s range is approximately +/- 2 billion. Unsigned int’s range is approximately 0 to 4 billion. Both are 4 bytes.

In ROBOTC V4.XX ints are 4 bytes, that’s the native size of storage on the STM32 processor.

In ROBOTC V3.XX ints are 2 bytes, this was probably for compatibility with the old PIC V0.5

The size of an int varies,however, short is usually 2 bytes and long is usually 4 bytes. To avoid confusion for much of what I do I now use “standard types”, for example, uint32_t is an unsigned 32 bit variable.

you should use the following


int NumOfArrayElements = sizeof(MyArray)/sizeof(int);

Ok, that makes sense, thanks!