Data Types in C Language for Microcontroller Development

In programming, data types are fundamental and important concepts, especially in embedded system development (such as microcontrollers), where various data types in C language are indispensable. Mastering these data types not only helps us allocate memory efficiently but also ensures the efficiency and correctness of the program.

Data Types in C Language for Microcontroller Development

1. Introduction to Basic Data Types

In C language, data types are mainly divided into the following categories: integer types, floating-point types, and character types. Each type has different bit sizes and ranges, suitable for different scenarios.

char (Character Type)

  • Bit Size: 8 bits (1 byte)
  • Range: -128 to 127
  • Overview: <span>char</span> type is used to store a single character, such as a letter or symbol. It can also store small-range integers and is commonly used for handling character data.

unsigned char (Unsigned Character Type)

  • Bit Size: 8 bits (1 byte)
  • Range: 0 to 255
  • Overview: <span>unsigned char</span> is the unsigned version of <span>char</span>, which can only store positive numbers or zero. It is typically used to store byte data.

short (Short Integer Type)

  • Bit Size: 16 bits (2 bytes)
  • Range: -32,768 to 32,767
  • Overview: <span>short</span> is used to store smaller integers, suitable for memory-constrained situations. It occupies less memory than <span>int</span> and is suitable for storing smaller numbers.

unsigned short (Unsigned Short Integer Type)

  • Bit Size: 16 bits (2 bytes)
  • Range: 0 to 65,535
  • Overview: <span>unsigned short</span> can only represent positive integers or zero, suitable for storing small-range unsigned integers.

int (Integer Type)

  • Bit Size: 32 bits (4 bytes)
  • Range: -2,147,483,648 to 2,147,483,647
  • Overview: <span>int</span> is the most commonly used integer type, suitable for most computational tasks. It has a large range of numbers and is typically used for common integer operations.

unsigned int (Unsigned Integer Type)

  • Bit Size: 32 bits (4 bytes)
  • Range: 0 to 4,294,967,295
  • Overview: <span>unsigned int</span> is the unsigned version of <span>int</span>, suitable for situations where only positive integers need to be stored.

long (Long Integer Type)

  • Bit Size: 32 bits (4 bytes)
  • Range: -2,147,483,648 to 2,147,483,647
  • Overview: <span>long</span> type is similar to <span>int</span>, but in some systems, it is used to represent larger integers or to be compatible with 32-bit systems. It usually has the same bit size as <span>int</span>.

unsigned long (Unsigned Long Integer Type)

  • Bit Size: 32 bits (4 bytes)
  • Range: 0 to 4,294,967,295
  • Overview: <span>unsigned long</span> can only store positive integers or zero, suitable for larger integer values that do not require negative numbers.

long long (Long Long Integer Type)

  • Bit Size: 64 bits (8 bytes)
  • Range: -(2^64)/2 to (2^64)/2-1
  • Overview: <span>long long</span> is used to store very large integers, capable of representing values larger than <span>int</span> and <span>long</span> types. It is suitable for applications requiring a larger range of values.

unsigned long long (Unsigned Long Long Integer Type)

  • Bit Size: 64 bits (8 bytes)
  • Range: 0 to (2^64)-1
  • Overview: <span>unsigned long long</span> is the unsigned version of <span>long long</span>, representing a very large range, and can only store non-negative numbers.

float (Floating Point Type)

  • Bit Size: 32 bits (4 bytes)
  • Range: -3.4e38 to 3.4e38
  • Overview: <span>float</span> is used to represent single-precision floating-point numbers, suitable for storing decimal numbers, and is commonly used in scientific calculations.

double (Double Precision Floating Point Type)

  • Bit Size: 64 bits (8 bytes)
  • Range: -1.7e308 to 1.7e308
  • Overview: <span>double</span> is used to represent double-precision floating-point numbers, which have higher precision compared to <span>float</span> type, suitable for scenarios requiring high numerical precision.

2. C Language Code Example

Below is a simple C program that demonstrates how to use these data types in code. Each data type has corresponding comments to help you understand their usage.

#include <stdio.h>

int main() {
    // Using char type to store a character
    char c = 'A';  // Store a character
    printf("char type stores the character: %c\n", c);  // Output character

    // Using unsigned char type to store an unsigned character
    unsigned char uc = 255;  // Store an unsigned character, maximum value 255
    printf("unsigned char type stores the value: %u\n", uc);  // Output unsigned character value

    // Using short type to store small-range integers
    short s = -32768;  // Store a small-range negative integer
    printf("short type stores the value: %d\n", s);  // Output short type value

    // Using unsigned short type to store unsigned short integers
    unsigned short us = 65535;  // Store an unsigned short integer, maximum value 65535
    printf("unsigned short type stores the value: %u\n", us);  // Output unsigned short integer value

    // Using int type to store common integers
    int i = 2147483647;  // Store a large integer
    printf("int type stores the value: %d\n", i);  // Output int type value

    // Using unsigned int type to store unsigned integers
    unsigned int ui = 4294967295;  // Store maximum unsigned integer
    printf("unsigned int type stores the value: %u\n", ui);  // Output unsigned integer

    // Using long type to store long integers
    long l = 2147483647;  // Store a large integer, suitable for long type
    printf("long type stores the value: %ld\n", l);  // Output long type value

    // Using unsigned long type to store unsigned long integers
    unsigned long ul = 4294967295;  // Store an unsigned long integer
    printf("unsigned long type stores the value: %lu\n", ul);  // Output unsigned long integer value

    // Using long long type to store larger range integers
    long long ll = 9223372036854775807;  // Store maximum long long integer
    printf("long long type stores the value: %lld\n", ll);  // Output long long type value

    // Using unsigned long long type to store unsigned long long integers
    unsigned long long ull = 18446744073709551615U;  // Store maximum unsigned long long
    printf("unsigned long long type stores the value: %llu\n", ull);  // Output unsigned long long integer value

    // Using float type to store floating-point numbers
    float f = 3.14f;  // Store a decimal number
    printf("float type stores the value: %.2f\n", f);  // Output float type value, keeping two decimal places

    // Using double type to store double-precision floating-point numbers
    double d = 3.141592653589793;  // Store a more precise decimal number
    printf("double type stores the value: %.15f\n", d);  // Output double type value, keeping 15 decimal places

    return 0;
}

3. Output Example

char type stores the character: A
unsigned char type stores the value: 255
short type stores the value: -32768
unsigned short type stores the value: 65535
int type stores the value: 2147483647
unsigned int type stores the value: 4294967295
long type stores the value: 2147483647
unsigned long type stores the value: 4294967295
long long type stores the value: 9223372036854775807
unsigned long long type stores the value: 18446744073709551615
float type stores the value: 3.14
double type stores the value: 3.141592653589793

Leave a Comment