Microcontroller Embedded C Programming Lecture 23| ‘C’ Integer data types and value ranges

  • Post author:
  • Post category:Blog

 

‘C’ Integer data types and value ranges

 

 

In this article, let’s understand ‘C’ integer data types, their storage sizes, and value ranges. 

Figure 1. ‘C’ integer data types, their storage sizes and value ranges
Figure 1. ‘C’ integer data types, their storage sizes and value ranges

 

‘C’ integer data types, their storage sizes, and value ranges as shown in Figure 1.

  • The signed char data type consumes 1 byte of memory, and its range is -128 to 127. 
  • A variable of type unsigned char also consumes 1 byte of memory, and its range is 0 to 255. That means that variable can store the value between 0 to 255. 
  • The size of the short int type variable is 2 bytes, and the range is -32,768 to 32,767. Sizes of ‘int’ and ‘long’ depend upon the compiler and the machine for which the code is going to be generated.
  • And unsigned short is also 2 bytes, int consumes 4 bytes, unsigned int also 4 bytes, long consumes 8 bytes, and long long consumes 8 bytes.

 

What is the meaning of that memory size table? 

The compiler(e.g., GCC) will generate the code to allocate 64 bits for each long long variable. 

‘C’ standard doesn’t talk about the memory sizes. The compiler designer fixes these memory sizes. For example, a variable of type int may consume 2 bytes on some compilers, and on some compilers, the size may be 4 bytes.

The same is true for a long data type. Some compilers consider the size of the long as 4 bytes, and some compilers consider it as 8 bytes. This all depends on the compiler. So, everything depends upon the way the compiler is designed.

 

For example, there is one compiler called XC8. XC8 is a cross compiler used to generate code for PIC 8-bit microcontrollers. And I got this table from the reference manual of that compiler. So, to know the memory size of different data types, you should consult a  reference manual of the compiler. For every compiler, the designer will give the reference manual.

Figure 2. XC8 is a cross compiler for PIC 8 bit microcontrollers
Figure 2. XC8 is a cross compiler for PIC 8 bit microcontrollers

 

Here you can see that((Figure 2)  the int size is 2 bytes or 16 bits. And the size of the long is 32-bits or 4 bytes. And this compiler also includes some non-standard data types such as short long, and other things. So, the short long is 24-bits, so this is not actually from the ‘C’ standard; it may be customization added by the compiler designers of XC8, which is designed by microchip.

 

Now,  armcc is another cross compiler developed by the ARM for 32-bit ARM based microcontrollers. This is not an open source compiler, and this is from the documentation of armcc. Here we can see that(Figure 3) char is again 1 byte, short is 2 bytes, and int is 4 bytes here, and long is 4 bytes, and long long is 8 bytes.

Figure 3. armcc cross compiler for 32 bit ARM based microcontrollers
Figure 3. armcc cross compiler for 32 bit ARM based microcontrollers

 

I’m showing all these tables because just don’t take memory sizes for granted for different data types. You have to refer to the reference manual of the compiler. 

 

‘C’ integer data types, their storage sizes, and value ranges

→ The C standard does not fix the storage sizes of different data types. It only talks about minimum and maximum values.

→ For example, the C standard says the minimum storage size of a long type variable is 32 bits, and the max is 64 bits. So, the exact size of the long type variable depends on the compiler design. 

→ Some compilers fix 32 bits storage size for long type variables, and some compilers fix 64 bits. Same is true for int data type, Some compilers fix 16 bits storage size for int type variables and some compilers fix 32 bits.  

This all depends on the compilers.

 

These data types will always be fixed size irrespective of the compilers. Short is always 2 bytes, char is always 1 byte, and long long is always 8 bytes. So, that is guaranteed. Whether you are using a cross compiler for embedded systems or a non-cross compiler, it doesn’t matter. Short is always 2 bytes, and the char is always 1 byte.

In the following article, let’s understand Integer data types one by one.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.

Leave a Reply