- A variable is a container for some type of data.
In the C language that container holds an integer,
floating point, character, or other value.
But philosophically speaking, what actually is a variable?
In this movie I help you explore the concept of the variable.
You'll see various ways a variable is described,
how to determine its size and its storage location.
In the typical C language variable declaration
you learn two tidbits about the variable.
Its type and its name.
Here you see an integer variable declared.
It's an integer variable, int, and its name is a.
The variable is uninitialized or never assigned a value.
Even so, you can use it in the code as is shown
in line 7, Build and Run the program.
On my screen, the value of a is equal to 50.
You might see the value zero, although that's just random.
Unlike other programming languages,
C does not initialize variables as they're declared.
Internally, the program allocates a chunk
of memory to store the variable's information.
That chunk isn't initialized, it's not set to zero.
It's just some location in memory and whatever value
is already there is immediately absorbed
by your variable as shown in the output here.
The moral of the story is to always
initialize variables before you use them.
You can make a fix in this code by adding a line.
Save that change, then Build and Run.
Now the output is predictable.
You also know something else about the variable, its value.
Two additional tidbits about the variable can be obtained
by using special C language operators.
The first is the sizeof operator.
Sizeof is a keyword, but it's considered an operator.
What it does is to return how many bytes
of storage are used by a specific variable.
That information may seem trivial,
but it does come into play often in the C language.
Open Exercise file Var2.
Variable a is not initialized here but that's not an issue
because it's not used, the sizeof operator
is used in the printf state.
The sizeof operator simply evaluates variable a
as an int variable to return the storage space it occupies.
The sizeof operator returns a long, unsigned integer value.
A placeholder required for that value is %lu.
Build and Run.
On this system an integer occupies four bytes of storage.
That's typical today but 20 years ago,
an integer used only two bytes of storage.
Change int to char in the code,
both at line 5 and in line 7 in the printf function.
Save, Build and Run.
A character occupies only one byte of storage.
Now change to float to see how big that variable type is.
Save, Build and Run.
On this system a floating point variable
occupies four bytes of storage.
A long integer or long int value is designed
to store really huge integers.
To see how big it is, change float to long in the code.
Save, Build and Run.
On this system, a long integer occupies
four bytes of storage, which is the same
as for a regular integer, but on some systems
you may actually see eight bytes of storage used
for a long integer value.
Likewise, a double value has twice the precision of a float.
Change long to double in the code.
Save, Build and Run.
Here you see the double does indeed
occupy eight bits of storage.
By the way, you don't need to change the placeholder
for any of these redeclarations.
That's because the sizeof operator is
what returns the long unsigned integer value,
not the variable itself.
Other information you can gather about a variable
includes its memory location.
That tells you specifically where the variable's data,
that one, four or eight bytes of storage,
where that data is located.
To fetch a variable's memory location,
use the ampersand operator.
You've already seen this operator used in the scanf
and timef functions if you've watched earlier movies.
Open exercise file Var3.
Three variables are declared in this code,
an integer, a character, and a floating point value.
These variables are not initialized because they are not used.
But the program does allocate space for them
at various locations in memory.
To access those locations, the &
is prefixed to the variable name.
To display the address, use the %p placeholder
in the printf statement as shown at lines 10, 11, and 12.
Build and Run this code.
The address value's output will differ
from machine to machine and even their format
will appear differently on different
computers and different operating systems.
Here you see the memory locations for those three variables.
At this point the information may seem trivial,
but it does play a major role when it comes
to pointers which are perhaps the scariest,
most dreaded thing in all of the C language.