Photo by Jeremy Bishop on Unsplash
Footprints in the Mind of the Computer: An illustrative explanation of Variables and Data types
The following concepts are dealt with explicitly in this article:
Variables and how to use them
Data types and their relationship in the computer's memory
What Structs and Unions entail
Oftentimes, when I watch Korean movies, I wonder why they so much love drinking liquor in tiny and beautifully designed cups. Well, my younger sister told me drinking is more fun if exercised in bits; I beg to differ from this as I prefer engaging mugs while savouring the taste of my fruit juice. Another interesting culture is the use of spoons to scoop pepper soup in order to provide it with a safe passage to the throat!!
All these illustrations are pointers to the fact that pleasure, aim, quantity and substance are fundamental to how human beings drink and eat. In allusion to the illustrations cited above, a variable can be pictured as a container; a spoon which holds a particular substance to be used. Semantically, a variable is a piece(s) of memory in your computer that consists of contiguous blocks of bytes. Is the word bytes scaring your understanding? If yes, kindly look up my article on the computer memory ojonilemi.hashnode.dev/how-much-can-your-co... Let's move on.
Imagine a world in which humans build the different sections of a house as standalone on the same piece of land. I mean, building your sitting room, toilet, dining room, etc. as separate entities on the same land. Memory optimizers reading this will be overly worried about the magnitude of land wastage that this imagination portends.
Hence, the same concept of having an edifice with different rooms carved in it is also the same way the computer memory is structured.
In consonance with my illustration, the rooms in your houses can also be abstracted as variables. The rooms have sizes which are a function of what the rooms are used for or contain; this ushers us into the world of Data types.
Inside most buildings, the living room takes the largest chunk of space because most of our entertainment gadgets are stationed in it; invariably, the toilet and store battle for the least space because of their magnitude of importance. Hence, datatypes are like these rooms ordained with different sizes and are used to perform varying degrees of importance. We have different types of data types in the computer and their sizes are largely machine-dependent. The basic data types we have are:
- Integers
- Floats
- Double
- Characters
- Wide Characters
- Boolean
- Complex Numbers
- Enumerations
#include <stdio.h>
#include <math.h>
printf("Size of Integer in bytes: %d",sizeof(int))
printf("Size of Character in bytes: %c",sizeof(char))
printf("Size of Double in bytes: %ld",sizeof(double))
printf("Size of Floats in bytes: %f",sizeof(float))
printf("Size of Boolean in bytes: %d",sizeof(bool))
printf("Size of Complex Numbers in bytes: %d",sizeof(complex)
The major differences among all the data types enlisted are the type of data they store and the memory size allocated to them by the computer. Just like you can call a toilet a restroom, convenience, ladies, comfort room etc. Relatively, this opens the door to giving variables names that are meaningful and explain the type of data they are used to store. Below is how you can name and initialise variables:
#include <stdio.h>
int number = 6;
char first_letter = 'a';
float score = 1.23f;
In addition, in programming languages like C and C++, you'll often hear of terminologies like: short, long and long long. These are mere prefixes that provide us with the volition to manipulate the size of our data types. It's something we see in the real world; different houses have different room sizes based on the owner's preferences. An example is stated below:
#include <stdio.h>
long unsigned int number = 6L;
char first_letter = 'a';
long double score = 12345677654;
Due to the low amount of houses in university areas, students are forced to adapt to a life of single rooms. These single rooms serve as the student's dining room, bedroom, kitchen, living room and store. Obviously, the toilet and bathrooms are always general-purpose facilities. Relatively, infrastructures like these exist in both C and C++ too. This infrastructure is called a "Union". A Union is a variable with the same memory address mapped to the variables it houses. It looks something like this
union person
{
char *fname; // variable to hold firstname of the person
char *lname; //variable to hold lastname of the person
int age;
float balance;
};
Another variation of this is a Struct, it's just like a self-con; which tries to shrink a house into a cubicle. A struct provides a way of housing different data types in a variable. It looks something like this:
//A structure declaration of a student
struct student
{
char *fname; // variable to hold firstname of the person
char *lname; //variable to hold lastname of the person
int age;
int matric_no;
};
What are the differences between a struct and a union? A Union has the same memory address allocated to all the data types it contains. The size of this memory is dictated by the datatype with the largest memory size. A struct has corresponding memory sizes mapped accordingly to the datatypes specified by its members. Another great difference is that you cannot use a union to store different types of data at the same time. It's just like saying that you want to use your kitchen as your bedroom while you're still cooking inside the kitchen, that'll be crazy right!!!!. Structs give you the volition to do this because of the exclusiveness of their members from one another offered by the allocation of different memory blocks to them. Below is a pictorial insight to these differences:
Variables and datatypes are two greatest inventions which have crowned our programming world with the ability to store, make permanent and implement solutions that ordinarily would have been impossible if they were non-existent. Thanks for reading, I look forward to your comments.