2. The bits and bytes behind variables and data structures.
What is a variable?
In C programming, each variable has a specific data type attached to its declaration. The data type tells us the size, range, and type of data the variable can store. We have 6 different variable types: short, int, long, double, float, and void.
A variable is data that can change value over time. Data for variables can be from a user or any other program including the program itself. Variables are stored in your computer memory that has a name associated with it called a reference. The content of the memory location is not fixed. When a value is assigned to a variable, the value is copied to the specified memory location.
When variables are bundled together with other functionality it’s called a data structure. Before we go off the rails and start discussing data structures let’s make sure we have variables completely understood.
Declaring a Variable
Making any meaningful program means that you’ll need to declare a variable somewhere along the way. Of course, when you create a variable it needs to have a value. Some programming languages let you get away with variables without a starting value but C doesn’t give you that freedom. By creating a variable name we declare a variable, and by giving it its first value we initialize the variable.
Let’s try an example:
The code above is totally editable, you can change the sentence or the value. Try it on your own and hit the run button.
So now you might be wondering how this happened. We’ll go step by step to make sure we understand every part.
#include
int main(void) { ~your code goes here~ }
We’ve already discussed these lines in the hello world tutorial, so if they look foreign to you, you should take a minute to read that tutorial first.
int money; // declaring our variable
In this part we declared our variable and we gave it a name: money. You can choose whatever you want as your variable. By using comments (//), we explained our code by adding some description. Remember comments in the previous tutorial? If you don’t, take a look at the hello world tutorial.
money = 5; // assigning and initializing our variable
After declaring our variable, we need to assign, define, and initialize our variable. Without assigning our variable, we get a null value which is the same as having the value of 0.
printf("I have %d dollars in my pocket.\n", money);
The print function gives your program the ability to output whatever text you put into its parenthesis. In this case, we give the function “I have %d dollars in my pocket“ as a parameter which it uses to output into the console. You might have two questions right now. What is %d? and why have we added the variable money at the end?
%d is a format specifier that basically says that wherever we put %d in our sentence, we wish a number to appear. But how can C programming tell which values we want to be printed? Well, we use our assigned variable money at the end of parentheses to specify that.
Also, you can implement the declaration and assignment of a variable in a single step by adding your value after int money = 5;
Exercise:
Try declaring and assigning a value to a variable in one line. Does it change your output?
Create a new variable with the type float and print it out using the same method as above. How does that change your output?
Naming variables:
The name of the variable is one of the most vital information concerning that variable. When naming a variable, you need to take into consideration the following:
Choose a convenient name for your variable to enable another person to debug your code, e.g., n1 is not a proper variable name but first_number is; you must make sure that the variable name is accurately descriptive and understandable to another reader.
Variable names must begin with an alphabet (a-z or A-Z) or an underscore (_)
Variable names are case-sensitive which means that a variable named Number is different from a variable named number
Special characters or spaces cannot be part of a variable name.
A Keyword cannot be used as a variable name.
Examples:
Valid variable names:
Jane
first_name
may_31st
a
_global_pandemic
Invalid variable names :
1st_user
%_of_population
v@lue
int
*kawaii*
As you can see, all of our valid variable names start with alphabets (a-z or A-Z) or underscore (_).
Can you tell why the second list’s names are all invalid? 1stuser is invalid because it starts with a number. %_of_population has special character: %, and also it doesn’t start with an alphabetic character or underscores. How about the other ones?
Do you remember we talked about declaring variables? Let’s get into more details.
The variable’s data type depicts the type of information that the variable can contain; it also determines the size of memory the variable can occupy. As we get into writing efficient programs we need to take memory into account. For now, it isn’t a big deal but keep that in mind for future tutorials.
After declaring a variable, we need to write information into that variable. Assigning a value to a variable, we use the = symbol with the variable name on the left and the value assigned to the variable on the right.
Note: The assignment operator ( = ) operator is NOT the same as the equality operator (==).
The Scope of a variable
The scope of a variable tells where the variable can be used within a program; that is, where the variable has been declared. A variable scope is depicted by a code block (a delimited by { } ). Although a code block is a universal programming term, it isn’t expressed the same way as it is in C for every language, so don’t be surprised if you read another language and it looks a bit funky.
Variables can either be declared as global or local variables.
A local variable is declared within a block or a function.
{
int first_number;
first_number = 12;
}
While a global variable is declared out of the block.
int first_number;
{
first_number = 12;
}
All blocks in a program can use global variables while a local variable can only be used within the block it’s declared in.
int first_number;
int sum_of_two_numbers;
{
int second_number = 5;
first_number = 12;
Sum_of_two_numbers = first_number + second_number;
}
In the code above, the two numbers are computed because they are computed within one block.
Now let’s take a look at the code below. Do you think it would run successfully? Copy the code and paste it in the code editor above to see if it works. If it doesn’t work, make an attempt to fix it!
int first_number;
int sum_of_two_numbers;
{
int second_number = 5;
first_number = 12;
}
Sum_of_two_numbers = first_number + second_number;
Primitive Data Types
As we mentioned earlier in this tutorial, each variable must have a specific data type attached to its declaration. The data type tells us the size, range, and type of data that the variable can store. The C programming language has primitive data structures which can be classified into the following parts:
The Integer data types: short, int, and long.
Floating-point data types: float and double.
The character data type: Char.
You’ll notice that each data type has a range with some funky characters. It’s important to note the range because it will let you know if that’s the data type you should use for your variable.
When you see -2 ^ 15, it means -2 to the power of 15. In other words, -2 multiplied by itself 15 times (-2 x -2 x -2 ..etc).
When you see 3.4e(-38), it means to the power of as well but -38 has the base of 10. 3.4e(-38) is the same as 3.4 x (10^-38). Since the exponent is negative, the datatype is indicating how many decimal places it can hold.
Short
The short data type in C is a variant of the integer data type. The short integer data type was created to provide integer values with a narrower range than the int data type.
Int
The Compiler determines the number of bytes used to store a data type in C LANGUAGE. The Size of an int variable is 16 bits or 2 bytes and never more significant than long; the Size of an int variable depends on the bit size of a compiler and the OS.
Example:
An int can be stored in a 16-bit Turbo C LANGUAGE compiler for at least 2 bytes, with the minimum value being -2^15 or -32768 and the maximum value being (2^15)-1 or 32767.
Long
A long variable in C LANGUAGE must be at least 32 bits or 4 bytes in Size, depending on the bit size of the Compiler and the operating system.
The minimum value range stored in a long is -2^31, and the maximum value range is (2^31)-1.
Note: Long data types are used to store more significant numbers because they have a wider range than int.
Float
Numbers with fractional parts can also be stored in float data types. As a result, when an exact calculation is required, we use this data type. A float variable must have a minimum size of 32 bits (4 bytes).
The min and maximum value floating data types do not need to be remembered, but for those who are curious, the minimum range is 3.4e(-38), and the maximum limit is 3.4e(+38).
Double
A double variable must be at least 64 bits (eight bytes) in length. A data type variable of the double data type can store data with a minimum value of 1.7 e-038 and a maximum value of 1.7e+038.
Char
The char data type in C LANGUAGE is used to store character values.
When you use the char data type to store a character value, you’re storing the binary equivalent of the character’s ASCII value rather than the character itself.
For instance:
The binary value of the ASCII value of A, i.e., 65, is stored in the system using this code. If 65 can be stored in a char data type, then a negative integer value can be stored in a char data type.
Bool
A bool data type could take either of the two values, i.e., 0 or 1, where 0 represents boolean value false and 1 represents boolean value true.