Embedded Development Board Learning


Data Types and Variables

Date: agosto 25, 2025

Author: Guillermo Garcia

Categories: Embedded C programming Tags:

Data types and variables are essential for embedded C language. In this article, we will learn about data types and variables.

Variables

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.

Identifiers

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.

keywords exclusive to the C language

Variable initialization

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;

Data types

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.

data type table

Example

#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:

  • j = 5 / 3; the variable j is defined as type int. Therefore only integer values can be stored in this variable. As the division of 5 by 3 equals 1.667, only the integer part 1 is stored into variable j.
  • strcpy(s, «Hello, world»); This is a function call to the standard function strcpy that copies the constant string “Hello, world” into the string variable s. The preprocessor directive “#include ” was added at the top to include the definition of the function strcpy into the program.
  • printf(«c = %c %d %o %x\n», c, c, c, c); This statement prints the variable c in 4 different formats. First the value of c is printed as a character (%c), secondly as an integer number in decimal format (%d). The conversion specifier %o is used to print c as an integer number in octal format and finally %x obtains the hexadecimal format of the same integer number.
  • sizeof C provides the operator sizeof to determine the size in bytes of a data type.

Memory size can vary depending on the microcontroller architecture being worked on; the most common in embedded systems are 32-bit architectures.

Summary

data type summary


Card image cap
Guillermo Garcia Thanks for reading! I am one of the editors of this site and I am committed to providing you with the best information possible by sharing my experience in embedded systems.


Comments... There are no comments.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Publicidad