Programming Basics: Variables
What is a variable?
Variables are perhaps the most basic and fundamental concept in programming. A variable is a data that changes throughout our algorithm, and this data has an alias by which we name it. For example, if you have ever played video games, you know that in some of them we have a health bar for a character, the character loses/gains health points throughout the game, and if it’s health points hit 0, it dies. Likewise, in a social media app, a user’s likes on a post are a variable. Anything that changes and its state has to be persisted throughout the program is a variable.
Why the C programming language?
Note: Low-level means programming languages that require more laborous work and do not hide away any details required to “talk” to the machine. They are “low-level” because they are closer to the hardware of the machine. High-level languages are more abstract, and are easier to use as you don’t have to worry about many things you would actually need to do when “talking” to a machine. These terms have nothing to do with better or worse programming languages, they only label languages based on their level of abstraction.
In this blog series, we use the C programming language to try to implement the basics of programming and learn them. You may not be motivated to learn C nowadays as everyone’s talking about modern stuff, like Python and Javascript, but don’t get tricked by that. Learning C will be very beneficial throughout your whole career. There are some concepts which modern languages are void of, which you will learn using C. And even though their absence is intentional, and arguably, in many cases, beneficial, in some cases, especially in the case of trying to grasp the essence of programming, their absence can be detrimental.
For example, in Python, you wouldn’t grasp the concept data types as well as in C, as Python isn’t a strongly typed programming language. Likewise, you wouldn’t know anything about memory management. In Python, you have a garbage collector that automatically allocates and releases dynamic memory for you (a relief in practice) which would limit your understanding of the interaction between the program and the heap.
Another important concept you will learn is pointers. High-level languages such as Python or Javascript do not have pointers. The pointer logic will appear in many instances in your career, and if you know pointers you will have an easier time understanding those things. Lastly, you will understand what it means to compile a program and run it. You will understand what compiled languages are. Python is an interpreted language, which -contrary to the popular belief- is an older approach to running programs. Although both approaches have their pros and cons, it is useful to know how both work. Anyhow, even though we will proceed with C, almost everything you learn here is applicable in any other programming language, as these are the basics.
Data types and memory
Now back to the topic of variables. Variables are stored in our program memory, specifically called the stack memory. Stack memory is allocated for our program upon running it. The stack memory has a predetermined capacity that may not be surpassed (otherwise an error known as stackoverflow occurs).
When you want to create a new variable, you need to declare its data type. Variables hold data, but not any data, only the data that has the pre-determined data type of the variable. For example, if you have an int (short for integer) variable, you may not put a character inside. If x is declared as int, it will only be able to hold an integer, and you can never change it. The two most commonly used data types are int and char. Char is used for holding a character as you might’ve guessed.
Variables use-cases
Note: When we use “//” in our code, whatever comes after it is ignored by the compiler and does not have any impact on our program. That is why we call this a “comment”. It’s just a comment that is useful for the reader of the code, but the machine completely ignores it.
The main idea of variables is that they vary. So generally, when you have created a variable, you will change it throughout your program. To identify where you need a variable, you need to look for moving parts in your algorithm, those that require change and “memory”. If you happen to have a thing that takes on different states throughout the algorithm, you probably need a variable for it. Let’s see some code examples:
int a; // declaring the variable as integer
a = 5; // assigning the variable to 5
// if we print a, it will be 5 here
a = 7; // assigning the variable to 7
// if we print a, it will be 7 here
a = 't'; // does not work at all (doesn't compile)In C, it is technically possible to assign a char to int, but this does not assign the char itself, but its ASCII code inside the integer. If you are not doing conversions, you needn’t worry about this, use char for characters and int for integers. Likewise,
char b;
b = 'c';
b = 10; // doesn't work as expected, assigns the character that has the ASCII code 10If this whole ASCII thing is confusing for you now, you can just accept it as is and we’ll explore it later. For now, what matters is that you understand variables and data types.
Now, without learning control statements in programming, variables cannot be fully explained, because we do not get to implement an algorithm that goes through various states. That is why only once we learn looping and the if statement we can understand variables fully.
Get Muhamed Cicak’s stories in your inbox
Join Medium for free to get updates from this writer.
For now, we can stick with simple examples:
int a = 5; //notice that you may assign a value WHILE declaring the data type of the variable. If you do not do this, your variable will be assigned to what is known as garbage value, which is basically a “random” value. What it really is, is the current value in the memory where our variable has been allocated.
int b = 7;
int c;
c = a + b;We can use arithmetic operations. In this context, a and b are called operands and the + is called the operator +. In the expression,
c = a + b;the values of the variables are used as operands,
So what we really get is,
c = 12;You can do the same with other operators such as *, /, — etc.
Later on, when we start implementing algorithms, we will encounter cases where we need to step through a list, one element at a time (like with maxElem function or sorting a list). Looping through a list basically means having a variable that represents an iterator, which we increase by 1 on every iteration. You will understand this better later, don’t worry.
Assuming we have a variable x, if we want to increase it by one, we can write the following statement:
x = x + 1This basically means: “the new value of x is going to be the current value of x plus 1”. It’s very self explanatory. Here, we could have written 5 instead of 1, in which case our variable would have increased by 5. There is a shortcut to this expression written as:
x += 1Those two are identical.
It is very common to want to increase your number by 1, which is why we have a special, short way of writing that. We actually have two different ways, and they differ subtly:
++x
x++Both of these result in x increasing by one, but there is an important difference. When we have an expression like:
c = a + bWe know that here, the operands are the values of a and b. So, what happens is that BEFORE the arithmetic operation is applied, a and b are EVALUATED, which basically means they are treated like their values, so that we can actually sum it. Now, we can use x++ and ++x on the rhs (right hand side) of the assignment as well, only their evaluation is going to be different. Let’s look at this example:
int x = 5;
int y = 7;
int c = x + y; // c = 12
int d = x++; // x is evaluated with its current value AND AFTER THAT incremented so: d = 5 and x = 6
int e = ++x; // x is FIRST incremented then evaluated so x = 7 and e = 7
int f = x + y++; // x is just evaluated and summed with y's current value, AFTER THAT y is incremented, so f = 7 + 7 = 14 and y = 8
int g = x + ++y; // y is first incremented, then x is evaluated then y is evaluated, so y = 9 and x = 7 + 9 = 16
// after all of the above code is executed, here, we have x = 7, y = 9, c = 12, d = 5, e = 7, f = 14 and g = 16Given the above example, you can get a good understanding of the pre-increment and post-increment syntax.
For now, we have covered enough regarding variables, we will focus on control statements in the upcoming blogs, which will consequently complete your understanding of variables.








