Embedded C: Data Types

Data represented in memories of micro-controllers will be in the form of ones and zeros (electronic bits @ physical level), in binary format.  To make sense of this data, they have to structure together based on the type – numbers or characters. C identifies/interprets this data based on the data type. Data type is rule set that states how to make sense of ones and zeros. Typically data types have predefined characteristics:

  • memory size to be allocated
  • range of values for that data type
  • operation that can be performed (addition, subtraction, etc..)

Embedded C already provides the built in primitive data types namely –

  • Character – to stores character constant(single character)
  • Integer – to stores numbers without decimal
  • Float – to stores decimal numbers

Primitive data type’s range and memory size can be altered with qualifiers, C provides below qualifies that can be appended to primitive data types

  • Signed
  • Unsigned
  • Short
  • Long

Size of the data type varies with the complier, for 16 bit complier above memory size is valid. For 32 bit compiler memory size of data type int will be 4 bytes instead of 2. But in all cases, int size will be minimum 2 bytes.

Usually compliers are designed for type of microcontrollers, to generate the effective code to best utilize the architecture. In order to be in line with the microcontroller type (16 bit, 32bit, etc), complier implements the data types sizes according to representation provided by controllers.  

Word is usually a regular unit of memory for given microcontroller, ex.

  • for 8 bit microcontroller, a word is just 8 bits
  • for 16 bit microcontrollers, a word will be 16 bits
  • 32 bit microcontrollers, word size will be 32 bits

Compliers are designed in a way to achieve the maximum efficiency, by deriving from the underlying memory layout (like mentioned above: word size of microcontroller). Because word size will define how the variables are stored and accessed during computation (16 bit microcontroller will need 1 cycle to handle int data type, because word size will be 16 bits).

Apart from primitive data type C provides derived data types and user defined.

Derived data types are derived from primitive data types and they are

Data typesExplanation
ArraysIt is used to hold sequences of similar data type elements, which can be addressed by the same name. Adjacent memory locations used for storing values, and shall be accessed by unique index number.  

//num is a variable that will //hold 10 integer data items
int num[10];
ReferenceIts an alternate name for the variable. In the program either reference or variable can be used interchangeable to access/modify since both point to same memory location.  

int a;
a=100;
int &b=a; 

// a and b can now used
//interchangeble
PointerA pointer is a variable that can store the memory address of another variable. With the help of pointers, memory can be allocated or de-allocated to the variables at run-time, thus, making a program more efficient.  

int x = 1
;int *p;
// pointer var declaration
p = &x;
// p holds the address of
//variable x  

User defined data types are defined by users/programmers to define their identifier that will represent an existing data type.    

Data typesExplanation
StructuresStructure is a collection of different data types which are grouped together and each element in a C structure is called member. Structure can hold different data types in a group.  

// student is a structure,
//holding integer, char
//array data types
struct student
{
int a;
char b[10];
}
 
UnionUnion is also like a structure, which holds different data types together. Only difference is in terms of memory allocation, in structure memory is allocated to all the members but in union memory is collocated to only the biggest element. With this restriction,  only one member can be accessed at a time.  

// student is a structure, holding integer, char
//array data types
union student
{
int a;
char b;
}

//if it was structure,3 bytes //of memory would have been
//allocated, but since its
//union only 2 bytes of
//memory will be allocated  
EnumEnumeration is a special data type that consists of integral constants, and each of them is assigned with a specific name. “enum” keyword is used to define the enumerated data type.

// Jan, Feb and march will be //variables, assigned with
//default values unless
//explicitly assigned
enum MONTH { Jan = 0, Feb, Mar };     
Advertisements


Categories: programming

Tags: , , ,

%d bloggers like this: