Skip to content

section4_variables

Fábio Gaspar edited this page Jan 11, 2019 · 1 revision

What is a variable?

A variable is a human readable way to represent a block of memory. If you recall from previous section, the smallest block of memory is one byte. However, there are larger blocks allowing to store more data. In C, these blocks of memory consists of one or more contiguous bytes, typically 1, 2, 4 and 8 bytes. Other higher level languages might provide support for even larger blocks of memory.

Variables have a name, a specific type of data associated, and point to some block of memory in RAM. The type of data specifies how the data should be interpreted and how much space is needed. A integer number, a decimal number, text, or others.

Naming rules

There are some rules to name variables.

  • A variable can only include letters, underscores and digits.
    • !FEUP ❌
    • faCuldadE_Engenharia ✔️
    • I3Es ✔️
  • A variable must not begin with digits.
    • 10something ❌

It's also important to be aware that C is a case-sensitive language. Therefore, the variable example is different from Example.

Here is a simple example of a variable declaration.

int demo;

Primitive data types

As mentioned previously, it's up to the developer to determine how a block of memory should be interpreted. That is done through data types. In the C language there are several data types.

The primitive data types all represent numbers, and we can categorize them in two major sets:

  • Integer numbers
  • Floating point numbers

Integer

An integer is an... integer! So, you probably thought there's only one way to represent integers on C. In fact, there are several ways. Why? The first reason is regarding memory usage. Remember that C is somewhat a low level language, and efficiency matters (execution time and spatially). If I only need to represent numbers from 1 to 100, why use more than one byte of memory when that's enough to represent that range of numbers?! The more memory you use, the larger is the range of numbers you can store.

We have data types allowing 1, 2 and 4 bytes of storage for integer numbers. Respectively, char, short int, int and long. Long is a particular case because the size is not standard at all. On some systems you might get 4 bytes, while on others you might get up to 8 bytes.

Then, there's a special keyword that can be associated with the previous ones. It's unsigned and it means the storage data is an unsigned number. That allows to duplicate the range of numbers. For example, a char, which takes one byte, can represent numbers from -128 to 127. If you use unsigned char, you can store numbers from 0 to 255.

Below is a table that sumps up the data types for integer numbers. Table

Source

Just a small note about char. This data type uses one byte and it stores numbers. However, the main purpose of char is to represent alphanumeric characters, according to ASCII Standard.

Floating point

Soon, you will find problems where you need to represent non-integer numbers. For example, arithmetic operations involving divisions will likely result in non-integer numbers. The C language also offers data types to store these numbers, listed below: float, double and the not very standard long double. The difference between float and double is associated with range and precision.

You can pair the floatting data types with unsigned too.

void

Another primitive data type is void. This means nothing or can be used as a generic data type in more advanced use cases. A function that produces no value can have a void return type. It means the function doesn't return any value.

void foo() {
    
}

Declaring/Initializing variables

Declaring a variable consists of specifying the data type and giving the variable a name. This is a simple example of declaring a variable called a for storing integers.

int a;

The stament above just tells the type and name of the variable. We haven't set any value. However, the variable already holds some value, which is junk! This is very important to be aware of.

💡 Why does this happens? When you declared a variable, a block of memory from RAM is reserved and associated to that variable. Recall that a variable is just an abstraction to represent blocks of memory. There's no way to eletronically say that a part of the RAM is empty or off. The result is unitiliazed variables having some sort of value without any explicit initialization in your code. Imagine the address 0xfa. You run a program that uses this hardware address to store the number 10. Then the program exits and you execute your own program which declares a variable of integer type. Coincidentally, that variable points to the block of memory with address 0xfa. Then, your variable will have the value 10, which is the gargabe left by the previous run program. The memory could be somewhat cleaned, but setting unused bits to 0. However, that's an expensive operation might be unecessary. It's up to the developer to ensure the variable is initialized before using it. Compilers when cofnigured to will alert you when you access a variable that you didn't initialized explicitly, helping you to avoid unexpected behavior in your program.

To assign a value to a variable you use the assign operator, =.

int a;
a = 10;

You can also set a value on the same statement you declare the variable, using the same operator.

int a = 10;

You can also declare variables in a list form.

int a, b = 6, c, d = 3; // a,b,c and d are all int variables. Only b and d were initialized. a and c contain junk

💡 If you want to declare multiple variables and initialize them with the same value, you can do it the following manner:

#include <stdio.h>

int main() {
    int a, b, c, d;
    a = b = c = d = 10;
    printf("%d %d %d %d", a, b, c, d); // 10 10 10 10
}