Variables
Variable
A variable is the combination of a types data type and an identifier (name) that represents one or more memory locations used to hold a program's data.

Looking at the simple C program we saw two pages back, we can observe the three topics of this section in action.

VariableContainer.png

Line 7 shows an example of a variable declaration (actually two variable declarations). A variable is simply a container for holding data, where the container occupies one or more bytes in the microcontroller's memory (typically RAM).

A variable must be declared before it can be used, as shown above. The declaration of a variable consists of two parts:

  1. A data type (or just simply a type) that serves two purposes:
    • Tells the compiler how to handle or manipulate the data stored in the variable. In the example above, the word float is one of several data types we'll discuss later in this section.
    • Tells the linker how much memory to reserve to store the contents of the variable.
  2. An identifier (or name) that will be used to uniquely identify the variable whenever we want to access or modify its contents. The words radius and area in the example above are identifiers.
VariableIllustration.png

To the right is a visual representation of how three variables might be stored in the 16-bit wide data memory of a PIC24 family device (there wouldn't actually be gaps between them).

The first variable warp_factor is of type int which is defined as a 16-bit value in the MPLAB XC16 Compiler, so it takes one complete word (two bytes) of data memory.

The second variable letter is of type char which is almost always an 8-bit value - only compilers with Unicode support might define it otherwise. So, it takes up one byte, or a half of a word of data memory. If other char type variables were declared, a second one would be packed into the same word as the first, so both bytes would be occupied.

The third variable length is of type float which is usually a 32-bit value with most compilers. It takes up two full words, or 4-bytes of data memory. It's data is also encoded (not shown) in a modified form of the IEEE-754 floating point format, so it needs to be handled differently from int and char type variables.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.