A variable in C is a name given to a location in the computer's memory where a programmer can store and/or access data.
A variable that stores a particular piece of data can also be used as a kind of alias or placeholder for the value of that data, similar to how a variable behaves in a mathematical formula.
Before you can use a variable, you must declare it. A variable declaration establishes the name of the variable and its type. It's a way of telling the compiler something like "Reserve a space in memory which I will henceforth refer to as X, capable of storing an integer".
The syntax for a variable declaration looks like:
int numberOfLegos;
In the above example int
is the data type and numberOfLegos
is the variable name.
Variable names in C must conform to the following rules:
- May only consist of letters, digits, and underscores
- May not begin with a number
- Cannot be in the list of C keywords
Also, for the purposes of this class please stick to the following conventions:
- Begin variable names with lowercase letters
- Make an effort to use contexually meaningful names (i.e. if you're counting Legos,
numberOfLegos
is an excellent name) - Use an upper case letter to demarcate the start of each word in your variable name beyond the first
There are three categories of simple data types in C.
- Integers:
int
,long
,short
,uint
- Decimal numbers:
double
,float
- Characters:
char
Within the numerical categories the various data types are differentiated by the size or precision of data that can be stored in the associated memory location - bigger or more precise numbers require more memory. In this class we will stick to using int
when we need an integer and double
when we need a decimal number.
The declaration of the 3 data types we will use in the class look like:
// integer
int numberOfLegos;
// decimal
double averageTemperature;
// character
char middleInitial;
Variable assignment means to give the variable a value. To assign a value to a variable, use the assignment operator =
in between the variable name on the left and the value you wish to assign on the right. For example:
// integer assignment
numberOfLegos = 14;
// decimal assignment (both int and double can be negative)
averageTemperature = -10.5;
// characters assignment: note the enclosing '
middleInitial = 'V';
A variable is initialized as soon as it is assigned a value for the first time.
int numberOfLegos;
numberOfLegos = 14; // variable is now initialized
You can combine declaration and initialization into a single statement:
int numberOfLegos = 14; // this is equivalent to the previous example
In a program a scope is all of the variables that are visible at a particular location in the code. For a variable to be in scope at a location in code, it must be declared before the point where it is referenced and it must appear within the same block (sections of code between an opening {
and a closing }
) or in a containing block. A variable cannot be used if it is not in scope.
For example:
int main()
{
numberOfLegos = 2; // this is not ok - numberOfLegos was not declared yet
int numberOfLegos;
numberOfLegos = 10; // this is ok
{
double surfaceArea = 6.0;
numberOfLegos = 19; // this is ok - blocks inherit scope from any enclosing scope
}
numberOfLegos = 3; // this is ok
surfaceArea = 3.2; // this is not - surfaceArea is not visible oustide it's enclosing block.
return 0;
}