C – Variables and Data Types
A 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.
There are different types of variable, and we can choose one by defining its data type. Here is the list of data types below that can store integers.
|Data Types||Size in Memory||Integer Range|
|char||1 byte||-128 to 127|
|short or short int||2 bytes||-32,768 to 32,767|
|int||4 bytes||-2,147,483,648 to 2,147,483,647|
|long or long int||4 bytes||-2,147,483,648 to 2,147,483,647|
|long long or long long int||8 bytes||–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
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 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.
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 have positive values. Please refer to the table below.
|Data Types||Size in Memory||Integer Range|
|unsigned char||1 byte||0 to 255|
|unsigned short or unsigned short int||2 bytes||0 to 65,535|
|unsigned int||4 bytes||0 to 4,294,967,295|
|unsigned long or unsigned long int||4 bytes||0 to 4,294,967,295|
|unsigned long long or unsigned long long int||8 bytes||0 to 18,446,744,073,709,551,615|
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.
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 stores them.
|Data Types||Size in Memory||Value Range|
|float||4 bytes||1.175494351e-38 to 3.40282347e+38|
|double||8 bytes||2.22507385850720138e-308 to 1.79769313486231571e+308|
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 .
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.
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 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.
Let us say we are creating a variable that represents the number of car that we own. Let us also say that we own 2 cars. We want to store that information to a program. The data type uint8_t would be ideal to use in this case. Please refer to the code below to see how we declare a variable that will store an integer of 2.
After seeing how we declare a variable, let us store integer 2 to that variable. There are two ways to do this.
Initialization means assigning value to a variable.
Multiple variables can be created in two ways. Let us say we have 2 cars, 1 truck, and 3 motorcycles. We are going to store that in memory. There are two ways to do this.
There is one person in an elevator that weighs 158.75 pounds (72 kg). Store that information in memory.
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.
There are two bank accounts that have no money in it. The two people who owns them just got paid $1000. To represent this in code form, please refer to the code below.
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 longer, break that line apart like the code below.
Now, let us see what happens if we try to store a real number that is not an integer to an int data type.
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.
We are going to see what happens if we are trying to store an integer to a float data type.
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.
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.
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.
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 compiler 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.
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.