Data Types and Variables in C

This post is teaching the different forms of data that can be stored in C.

Johnson Tang

The chart of ASCII codes and corresponding characters.
The chart of ASCII codes and corresponding characters.
After learning how to write our first lines of C code, we can now learn about the different types of data that can be stored in C. In C, developers are able to store values inside variables. Variables are basically a container which store values. These values can then later be used in code. In the last post, we have been introduced to integers and strings, but there are more data types in C. In this post, we will be going through a few more essential data types in C. These include floats, doubles, characters and arrays. But before we are able to understand how these data types are different, we must understand how C stores data to the computer.

Firstly, C stores data into a computer's memory. Memory is where a computer stores data that is going to be handled in the near future. When a program is run, the computer allocates memory to the program, and the data needed for the program is stored in the memory. Computers run of a binary system, meaning that they operate with a base 2 system. All data stored in computers is done with just ones and zeros. How you might ask? For example, if the computer would like to store a number, they are able to do that with the binary counting system. The decimal system, which we use to count, has digits increasing in base 10. This means that each digit is 10 times the value of the digit to its right. The binary system is similar, but instead of 10 times more the value, it is 2 times the value. For example, if we would like to write 7 in binary, we would write 111. This is as the first digit on the right is worth one, the middle digit is worth two and the left digit is worth four. One, two and four add up to seven. This is how computers store numbers, but how do they store characters? This is where the ASCII comes in. ASCII stands for American Standard Code for Information Interchange. ASCII is a set of codes that allow computers to store characters and represent text. Each character has a corresponding ASCII code, and the computer use ASCII to represent characters by using the binary numbers that correspond with the ASCII code.

Almost all modern computer and devices utilise binary.

Now how would a computer know if you are trying to just store a number or a character? In C, we must specify what sort of data we want to store in memory. This is where data types come in. Data types specify to the computer what sort of data the binary code represents. Firstly, an integer is a number stored by the computer. This means that the binary for the integer is just the binary equivalent of the integer specified. Each digit in the binary number representing the integer is one bit. A bit is the smallest unit of data in a computer, and it can only be a 0 or a 1. An integer is assigned 32 bits or 4 bytes in memory (8 bits = 1 byte). This means that it has 32 spots to store a binary nubmer. This these 32 spots, it its able to store a number from -2,147,483,648 to 2,147,483,647. A long integer is an integer but it is assigned 64 bits in memory or 8 bytes. This means that it is able to store a number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Next, a float is a number with a decimal point. It is assigned 32 bits in memory or 4 bytes. However, if developers require more accurate decimals, they are able to utilise doubles, which allocates double the amount of memory. Characters store a single character, and strings store a sequence of characters. All these variables can be re-defined after they are first defined later in the code, however if a developer would like to make this code unchangable after it is declared, they can use the const keyword in front of declaring the variable.

To store these forms of data, we must put the data type before the variable name. For example,

#include <stdio.h>
int main(){
    int variable1 = 5; // variable1 will be storing an integer
    long variable2 = 922337203685477580; // variable2 will be storing a long integer
    float variable3 = 3.14; // variable3 will be storing a float
    double variable4 = 3.14159265359; // variable4 will be storing a double
    char variable5 = 'a'; // variable5 will be storing a character
	const int variable6 = 5; // variable6 will be storing an integer, but it cannot be changed
}

Notice how we could store strings in a variable? This is due to C's lack of the string data type. To store strings inside a variable, there is a workaround. This involves arrays, which are a sequenced of variables. However, arrays are a topic for another post.