Primitive Variables

What are Primitive Variables

Variables are ways of representing data in programming languages. The adjective "primitive" just refers to the fact that primitive variables can usually be modified (addition, multiplication, etc.) in one instruction (as in, machine instruction, not c instruction) by the computer, and are therefore, the simplest forms of data that can be represented in both machine instructions, and in the C programming language.

Variable Declaration

Variable declaration is the action of telling the compiler that a new variable with a specific name has been created. Variables in C are declared like this (the correct way to name a variable was covered in the last two tutorial sections in the "Naming" sub-sections):

NEVER forget to put a semicolon at the end of a statement in a line of code, or else you will get a compilation error (which means that an executable will not even be generated).

Naming Reminder

Still, in case you skimmed over the last two sections, single-word variable names in C are by convention, lowercase. The first letter of every other (optional) word is, by convention, uppercase. Variable names in C also must not contain any spaces in them, otherwise you get a compilation error.

Variable Assignment

Assigning a variable (giving a variable a value) is simple. You just need to write the name of the variable, =, and the value you want to set the variable to. You could assign the variable declared above like this:

You can also declare and assign a variable in one statement like this:

Variables can also be assigned to other variables (you can also declare and assign a variable to another variable at the same time). The following two lines of code are syntactically correct:

Assignment does not need to come immediately after declaration. You can assign a variable at anytime within a program, as long as you have already declared the variable in a previous line of code.

Integer Variables

Types and Precision

The first type of variable that we are going to discuss are integer variables (variables that represent numbers without a decimal portion). There are four main types of integer variables in C:

Each type of integer variable represents a different type of precision. The precsion of a number (decimal variables also have precision) variable is the range of values it can hold, in the case of integer variables. The more precise an integer variable is (or, in other words, the greater the range of values it can hold), the larger its size in bytes. Exactly how large each type of variable is depends on the compiler and the type of processor you are writing code for. However, for x86 and x64 (Intel) computers, you can expect the sizes to be this:

Signedness

When dealing with integer variables, you can also add the keyword signed or unsigned before of after the variable type (but not after the variable name, and by convention, these keywords always coe before the variable type). A signed variable is a variable that can contain negative values, whereas an unsigned variable is a variable that cannot contain negative values. Because unsigned variables cannot hold negative values, they often can store larger numbers than signed variables. All the compilers I know make variables signed by default, so you don't need to write "signed" before variables that you want to be able to be negative. If you want the number to only be positive and be able to store higher values, on the other hand, then you must declare the variable "unsigned". The exception to this is the char integer type (which is often used to represent characters, in case you haven't noticed that from the name yet).

The C standard states that the default signedness for the char integer type must be the one that is most efficient for storing characters on a specific platform type (REMEMBER THIS: I once spent hours trying to find the cause of a program working incorrectly and it turned out to be because I changed from compiling for an x86 (intel) phone to compiling for a ARM phone, and unsigned is the most efficient signedness in ARM). On some processors, such as x86 and x64 (both Intel), signed and unsigned variables exhibit the same performance. On other platforms, on the other hand, unsigned variables are faster, so always declare you variables as unsigned whenever you can.

Here are the ranges for different types of integer variables on x86 and x64 (remember that variables are assumed to be signed if their signdness is not specified, in most cases):

The above ranges are all inclusive. Also, REMEMBER: numbers that you write in C cannot contain commas. The reason that the maximum values for unsigned integers are 2^(a power) - 1 instead of just 2^(a power) is that the number "0" occupies a space. This also explains why the signed integer variables don't have minimum and maximum values that are equal in magnitude (the fact that it is -128 to 127 instead of just -128 to 128). Note that the magnitudes of the minimum and maximum values for signed integers are roughly half the maximum value of their unsigned counterparts. This is because the bit that represents the sign in signed integers is instead used to add another bit (or multiply by a power of 2) to the maximum value of the integer.

The following code demonstrates how to print a variable onto the screen:

This is just like the original example, but the "4" in the printf function is replaced with the variable name.

Mathematical Operations

You can perform mathematical operations on variables almost just as easily as you would on written numbers on paper. The main difference is that you must set a variable equal to the result of the operations, or else the result is ignored by the compiler (since how else is the compiler supposed to know which variable stores the result?). For example if you wanted to add two variables together, you could do this:

This would print out "10"

You could even do this:

This would print out "10" as well. In this example, integer b was assigned to the sum of itself and integer a, and then integer b was printed onto the screen.

The following symbols represent mathematical operations that you can perform on integer variables:

Some of these operations may seem confusing to you. There is no need to worry, because the next section of this C++ programming tutorial will be about primitive-variable operations.