To share this page click on the buttons below;
Variables
C programs are made by variables and functions. For the moment let's keep it short: variables contain the data your program is made of, functions process variables to obtain the result you desire.
A variable is a labeled “jar” of data: each variable contains some kind of data (the value of the variable) and we can refer to that value using the label attached to the variable (the name of the variable). We can change the value of a variable (its content) but not its name.
In C every variable is of a certain type (C is a typed language): that means each “jar” can contains just some type of data and not another. Each variable is placed somewhere in memory and each type require a different amount of memory.
There are some good reasons C requires to declare the type of each variable.
The first reason is related to performances: to each variable will be reserved only the exact space in memory to contain the variable and no more. The second reason is that the operations you can perform on a variable may depend on the type of the variable (for example a division between integers is handled in a different way respect to the division between real values). The third and more subtle reason is related with the ability to discover earlier problems and errors. Once you declare the type of a variable you are making an agreement with the compiler: you are promising what that variable is going to contain. If you break this pact (and in complex project with thousands of variable this can likely happens) by assigning one variable to another which has been declared of another type, the compiler can complains and this allow you to discover errors in your program before they are actually executed.
Basic types
C language defines 4 native types:
char
to contain single characters of text (encoded as ASCII values, we will se about that later on)integer
to contan integer numbersfloat
to contain real number in sigle precisiondouble
to contain real number in double precision (we will see about precision later on).
That is just the big picture, each type can be “modified” using specific keywords (but again, we will see about that later on).
Declaration
A variable is declared when a certain variable name is associated with the type of the variable. The declaration is just a promise we make to the compiler: we promise somewhere there will be a variable with that name of that type, but with the declaration no space in memory is reserved for that variable.
We declare a C integer variable (and we name it a
) with the following statement:
int a;
int
tells the compiler that the variable a
is an integer (i.e. that variable can contain only integer numbers), ; (semicolon) tells the C statement is finished and it will appears always at the end of any C statement.
A statement is the shortest and complete portion of C code that has a defined meaning.
Definition
The definition of a variable makes the compiler to reserve the space in memory for the content of the variable. We define a C integer variable by initializing it to some value (here 5):
a = 5;
Actually the definition of a variable happens the first time the variable is used in some espression: pay attention to the fact that the content of an unitialized variable is random!
Initialization
We can declare and define a C integer variable at once:
int a = 5;
Always declare and define a variable at once: this ensure that the variable has a proper content the first time is used.
Use
Now we can use the variable in some expression (the following one sum 4 to the content of a
, which was 5, and put it again into a
, so that after this statement a
contains 9):
a = a + 5;
To share this page click on the buttons below;