Date: agosto 25, 2025
Author: Guillermo Garcia
Categories: Embedded C programming Tags: Embedded C programming
Data types and variables are essential for embedded C language. In this article, we will learn about data types and variables.
Table of Contents
A variable is a symbolic name used to address a certain memory location. The compiler will replace all variable names with their corresponding memory addresses. A variable has a memory location (lvalue) indicated by the variable name and a value (rvalue). The variable in the example in Figure 8 has the name number and a value of 10.
int number; number = 10;
lvalue and rvalue of a variable In memory 10.
Before declaring a variable, let’s consider the identifiers, which are words exclusive to the C language. We need to know them so as not to use them as variable names and avoid confusion.

Once a variable is defined, it corresponds to a certain memory location that is big enough to hold the specified type of data. However, the variable does not have a value (rvalue) yet. To assign a value to the variable, an assignment statement is needed.
In some cases, the value of the variable needs to be set before it can be used in a computation. The variable needs to be initialized. In the example of Figure 8, the variable number is initialized with the integer number 10 by the statement: “number=10;”. After execution of this statement, the value 10 will be stored at the memory location the variable number is assigned to. In C, a variable can be defined and initialized in one statement:
int number = 10;
All variables need to be defined with a name and data type before they can be used in a program. The data type will determine the number of bytes needed to store that variable in memory and the type of operations allowed on it. In the example, the variable with name number is defined to be of the type int, which means that it will hold integer numbers. Depending on the system you work on, the amount of bytes needed to store integer values will be 2 or 4.

#include <stdio.h>
#include <string.h>
int main(void)
{
int i, j;
char c;
float x, f;
double ff;
char s[64]; // s can contain a series of chars (=string)
i = 2;
j = 5 / 3;
c = 'A'; // 0100 0001
c = c + 1;
x = 5 / 3;
f = 5.0 / 3;
ff = 5.0 / 3;
strcpy(s, "Hello, world");
printf("i = %d\n", i);
printf("j = %d\n", j);
printf("c = %c %d %o %x\n", c, c, c, c);
printf("x = %20.17f\n", x);
printf("f = %20.17f\n", f);
printf("ff = %20.17f\n", ff);
printf("s = %s\n", s);
printf("\n");
printf("The size of an int is %d bytes.\n", sizeof(int));
printf("The size of a char is %d bytes.\n", sizeof(char));
printf("The size of a float is %d bytes.\n", sizeof(float));
printf("The size of a double is %d bytes.\n", sizeof(double));
printf("The size of the string s is %d bytes.\n", sizeof(s));
return 0;
}
Output of this program:
i=0 j=1 c=B 66 102 42 x = 1.00000000000000000000 f = 1.66666666662693023680 ff = 1.66666666666666666679 s = Hello, world The size of an int is 4 bytes. The size of a char is 1 bytes. The size of a float is 4 bytes. The size of a double is 8 bytes. The size of the string s is 64 bytes.
Key points:
Memory size can vary depending on the microcontroller architecture being worked on; the most common in embedded systems are 32-bit architectures.

Deja una respuesta