Skip to main content
\(\newcommand{\doubler}[1]{2#1} \newcommand{\binary}{\texttt} \newcommand{\hex}{\texttt} \newcommand{\octal}{\texttt} \newcommand{\prog}{\texttt} \newcommand{\lt}{ < } \newcommand{\gt}{ > } \newcommand{\amp}{ & } \)

Section4.1C/C++ Basic Data Types

The ARM architecture defines several data types for both the 32- and 64-bit implementations. The processor used in the Raspberry Pi is a 32-bit implementation. Table 4.1.1 shows the names for both implementations for completeness.

Name Size (bits) Architecture (32/64 bit)
Byte 8    both
Halfword 16    both
Word 32    both
Doubleword 64    both
Quadword 128    64-bit only
Table4.1.1ARM architecture data sizes.

C and C++ provide several data types. Their sizes for the 32-bit ARM architecture are shown in Table 4.1.2.

Data type Size Description
int Word Integer
long Word Integer
short Halfword Integer
char Byte Byte
long long Doubleword Integer
float Word Single-precision IEEE floating-point
double Doubleword Double-precision IEEE floating-point
bool Byte Boolean
void* Word Address of data or code
Table4.1.2Sizes of some C/C++ data types in 32-bit ARM architecture.

In the 64-bit version (not the Raspberry Pi) the sizes are the same, except the void* data type is a doubleword bits.

A given “real world” value can usually be represented in more than one data type. For example, most people would think of “123” as representing “one hundred twenty-three.” This value could be stored in a computer in int format or char format, with each char holding one of the characters in this number. An int in our C/C++ environment is stored in 32 bits, and the bit pattern would be\begin{gather*} \hex{0x0000007b} \end{gather*}As a C-style text string it would also require four bytes of memory, but their bit patterns would be\begin{gather*} \hex{0x31 0x32 0x33 0x00} \end{gather*}(Recall that a C-style string is terminated with a NUL character.)

The int format is easier to use in arithmetic and logical expressions, but the interface with the outside world through the screen and the keyboard uses the char format. If a user entered “\(123\)” from the keyboard, the operating system would read the individual characters, each in char format, and store them as a text string. The text string must be converted to int format. After the numbers are manipulated, the result must be converted from the int format to char format for display on the screen.

On the other hand, if the program primarily manipulates the numbers as text, rather than in arithmetic computations, it may be more efficient to keep them stored as text strings.

C programmers use functions in the stdio library and C++ programmers use functions in the iostream library to do these conversions between the int and char formats. For example, the C code sequence:

scanf("%d", &x);
x += 100;
printf("%d", x);

or the C++ code sequence:

cin >> x;
x += 100;
cout << x;

  1. Reads characters, each as a separate char from the keyboard and converts the char sequence into the corresponding int format.

  2. Adds 100 to the int.

  3. Converts the resulting int into a char sequence and displays it on the screen.

The C or C++ I/O library functions in the code segments above do the necessary conversions between char sequences and the int storage format. However, once the conversion is performed, they ultimately call the read system call function to read bytes from the keyboard and the write system call function to write bytes to the screen. As shown in Figure 4.1.3, an application program can call the read and write functions directly to transfer bytes.

<<SVG image is unavailable, or your browser cannot render it>>

Figure4.1.3Relationship of I/O libraries to application and operating system. An application can use functions in the I/O libraries to convert between keyboard/screen characters and basic data types, or it can directly use the read/write system calls to transfer raw bytes.

When using the read and write system call functions for I/O, it is the programmer's responsibility to do the conversions between the char type used for I/O and the storage formats used within the program. We will soon be writing our own functions in assembly language to convert between the character format used for screen display and keyboard input, and the internal storage format of integers in the binary number system. The purpose of writing our own functions is to gain a thorough understanding of how data is represented internally in the computer.