C – Variables and Data Types

Course Outline  | Previous LessonNext Lesson

Introduction

variable is a placeholder that holds information temporarily for future use. Variables work by having them referring to physical locations in memory. Information can come from user inputs, file read, and more.

Data Types

There are different types of variable, and one can be chosen by defining its data type. Here is the list of data types below that can store integers.


Programmers use the smallest data type they can to save space. The table above shows how big and small an integer can be per each data type.  If an integer of 2017 needs to be stored in memory, data-type short would be used since that number is within short’s range. In addition, short’s memory size is the smallest for that number.

Keyword signed

Keyword signed can be typed to the left of a data type. Signed enables the variable to store negative and positive values. However, the data types are already signed by default so there is no need to type that in.

Keyword unsigned

The integer range can be extended in the positive direction if negative values are not needed. Keyword unsigned can be used to limit variables by having only positive values. Please refer to the table below.

Discrepancy

Data types’ size can vary from system to system. In some computer systems, data type int reserves 2 bytes instead of 4. We can solve this discrepancy by using the following library below.

#include <stdint.h>

Data type int can be set up as int32_t (data type defined from library above) if int is required to take up 4 bytes in all systems. Please note that there are 8 bits in a byte. So there are 32 bits in 4 bytes. Data type unsigned int can be declared as uint32_t if unsigned int is required take up 4 bytes in all systems as well.

Real Number Values

Real numbers can also be stored in C. The following data types below store them.

Remember to choose the smallest data type that is possible for a number that you want to store. For an example, use float for storing 2.25.

The lowercase e represents 10. That number 10 is being raised to the power of the number that is beyond e. For an example, 1.175494351e-38 is equivalent to 1.175494351 x 10^{-38} .

Data Type and Variable Format

When declaring a variable in C, it is required to set it up like the format below. Declaring means that you are introducing a variable to the program with a new name representing a memory space.

data_type variableName;

You can replace variableName with anything you would like. The variable could have been named strawberries instead. However, the variable name cannot start with a number. 1variableName would be invalid.

In addition, you can name your variable like the valid one above, or you can name it like variable_name if you have a variable name with two words. Those two are standard ways of declaring variables with multiple words in names.

Determine What Data Type to Use

Before creating a variable, decide what kind of information you would like to store. If you decide the variable will store the maximum integer of 200 for an example, the data type uint8_t would be ideal to use. Remember that the number you see from that kind of data type is in bits. Also remember that unsigned is being used. That variable would reserve a size of 1 byte, and the variable will only accept positive integers.

Coming Up with Variable Names

Another thing you need to do is come up with a variable name that best describe what it is used for. Note that everything in C is case sensitive. For an example, if you declare a variable with the name CaNdY, and when you need to use that variable, you have to type that in exact, upper and lower case letters.

Example 1

The first example shows a way of creating a variable that represents the number of cars that an owner owns. The owner owns 2 cars, and that information will be stored into a specific variable that will be declared. The data type uint8_t would be ideal to use in this case since integer 2 is significantly smaller then the data type’s maximum limit. Please refer to the code below to see how that variable is declared and how the integer of 2 is stored to that particular variable.

uint8_t cars;     // Declaration

After seeing how a variable can be declared, the program below shows an example of storing integer 2 to that variable. There are two ways to do this.

// First way
uint8_t cars = 2;     // Declaration and Initialization

// Second way
uint8_t cars;     // Declaration
cars = 2;        // Initialization

Initialization means assigning value to a variable for the first time.

Example 2

Multiple variables can be created in two ways. In this example, a person owns 2 cars, 1 truck, and 3 motorcycles. The owner of those vehicles want to store that information in memory. There are two ways to do this.

// First way
uint8_t cars, trucks, motorcycles;
cars = 2;
trucks = 1;
motorcycles = 3;

// Second way
uint8_t cars = 2, trucks = 1, motorcycles = 3;

Example 3

There is one person in an elevator that weighs 158.75 pounds (72 kg). Store that information in memory.

uint8_t persons = 1;
float weight = 158.75;

Example 4

Now, there are two people with bank accounts. One has $1000 and another has $0. A person transfers all his or her funds to another person’s bank account. Thus, both account balances swap.

int16_t bankAccount1 = 1000;
int16_t bankAccount2 = 0;

bankAccount2 = bankAccount1;
bankAccount1 = 0;

Example 5

There are two bank accounts that have no money in it. The two people who own them just got paid $1000. To represent this in code form, please refer to the code below.

int16_t bankAccount1 = 0, bankAccount2 = 0;

// Pay Day
bankAccount1 = bankAccount2 = 1000;

The code above works by assigning 1000 to bankAccount2 variable first. Then the data from that variable is copied to bankAccount1. However, if that line of code is too long that makes the code hard to read, break that line apart like the code below.

int16_t bankAccount1 = 0, bankAccount2 = 0;

bankAccount2 = 1000;
bankAccount1 = bankAccount2;

Example 6

This example shows what happens when a real number that is not an integer is being stored into an int, data-type variable.

int variable;

variable = 2.99999;     // "Rounds" down to 2

The values on the right hand-side will automatically convert to whatever data type is on the left-hand side of the equal sign. In this case, the program will covert 2.99999 to an integer. The program isolates the 2 from the real number since 2 itself is an integer. Then 2 is stored to memory, and the digits after the decimal point are neglected.

Example 7

This example show what happens if an integer is being store into a float, data-type variable.

float variable;

variable = 1;     // 1 Converts to 1.000000

The integer 1 converts to 1.000000 since that variable is a float on the left-hand side of the equal sign.

Equal (=) Signs in C

Please note that the equal signs you see above do not represent equality in C. It means that data from the right-hand side of the equal sign is an expression that is assigned to a memory space where the variable is linking to. The equal sign is an assignment operator because it assigns data to memory. The following code below is invalid in C.

uint8_t example;

200 = example;     // Invalid syntax

The code above does not make sense. The left-hand side of the equal sign is supposed to be the destination for data in memory. However, 200 is clearly not a variable, which makes the syntax above invalid.

American Standard Code for Information Interchange (ASCII)

Integers can be used to represent a character. Those integers are considered to be ASCII code. Here is the list of integers that represent characters. Data type char is specialized to store characters.

char A1 = 65;    // 65 = A
char A2 = 'A';    // 'A' = 65

As you can see above, there are two ways to store a character. When there is a character enclosed with single quotations, the computer automatically turns that character into an integer that represents that character. The character in between the single quotes is called a character literal.

Uses of char

There are two interpretation of the code above, and it depends on how programs use it. A programmer may want to use char to store integer 65 without representing anything to save memory space since it only takes up one byte, or the programmer may want to store integer 65 that does represent a character. Both way works for different purposes.

Variable’s Scope

There are two different types of scopes for variables. A global variable is declared outside of any function, and its lifetime lasts until the program terminates. Any function can utilize that variable. The variable’s scope is basically the entire program. A local variable is declared in a function, and its lifetime lasts until the function is out of scope meaning the computer reaches the close curly brace (}). When its lifetime is over, that variable does not exist any longer. Only the function that contains the local variable in its scope can utilize that variable.

// No need for libraries in this example since we are using data type int

int variableName1;     // Global Variable

int main() {
    int variableName2;     // Local Variable
    return 0;
}

Conclusion

Variables are used to store information for future use in memory. There are different types of variables that are defined by data types. Multiple data types can store integers, but each data types reserve different amount of size in memory. There a couple of data types that can store decimal numbers as well. A global variable can be used by any function in that program file, and the variable lasts until the program is done executing. A local variable can only be used in a function where it has been declared within that function’s scope. That variable lasts until the compiler is done executing that function. That variable would cease to exist. Use Code::Blocks to practice C using variables.

Course Outline  | Previous LessonNext Lesson

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz