C provides more access to low-level implementation details that is
typically afforded by high-level languages. This contributes to C's
flexibility and its power as a systems programming language, but it
also makes it possible to write obscure non-portable programs. It can
also lead to confusion on the part of beginning C programmers. In
this section we'll see a few examples of this low-level access.
C will allow a program to determine how many bytes are occupied by a
variable of a particular type. This capability is useful in one
situation--when a program needs to allocate space for a variable on
the heap. Since we not discuss heap allocation until later in the
course, this capability will not be any more of a curiosity to you
until then. But it is kind of fun to play with.
Take a look at the program ``sizes.c'' in your ``examples'' directory
(or view it directly). All
it does is to declare several variables of different types and the
calculate and display their sizes. If you run it, you can see how
much space is taken up by various kinds of variables.
Seven kinds of variables are represented: characters, three different
lengths of integers, and three different lengths of float point
numbers. Does running this program reveal anything of interest?
Click here for the answer
I want to emphasize that being able to determine the size of a
variable has absolutely no practical value at the time being.
Eventually, as mentioned above, we will find an application.
C will also let you determine the location of a variable in the
computer's memory. That is, at any point in a program's execution a
program can ask where a variable is currently located. Although we
will not have much use for this capability until we begin talking
about pointers, we have already used the capability several times as
you will see shortly.
Take a look at the program ``addresses.c'' in your ``examples''
directory (or view it directly). This program simply declares the same set
of variables as the last program, and then prints out their addresses
(instead of their sizes). Run the program. Do you notice anything
interesting?
Click here for the answer
Remember: this may be somewhat uninteresting, but this is because the
ability to calculate the address of a variable is not very useful at
the moment. As we mentioned above, however, we have already used the
address operator in some of our programs. Where was that?
Click here for the answer
Hamlet Project
hamlet@cs.utah.edu