2.1. The Primitive Data Types from C
There are actually five primitive data types present on the C language. These types are: char, int, float, double, and void, respectively. The other types are based upon these five build-in data types.
The size and range of these types tend to vary between processor types and compilers.
NS: When we define a data type we are literally applying restrictions on the way related to how the bits are grouped and to what kind of operations are possible to be performed on those grouped number of bits.
Example:
The next image shows, in a figurative way, the basic concepts behind type definition.
You can also use modifiers to change the numeric range and the size of base-types. These modifiers are: long, short, unsigned, and signed.
The next table, shows all C's primitive types (they are also present in C++,) as well as its size and range variation sometimes altered due to the use of the modifiers mentioned before:
2.2. Identifiers
Identifiers are names used to identify (:P) functions, variables, labels, and user-defined objects. These names can only begin with a letter from the alphabet or an underscore For example, let's observe the following sentences.
int value; // ok!
int _value; // ok!
int 2value; // error!
The first two definitions are both valid, and if we would put that at our code we would be creating two identifiers that would describe two specific variables that are meant to store integer values.
As stated before, identifiers must exist also for functions, tags, and user-defined objects, but the last application example for varibles is enough to make you understand the principles behind identifiers uses and definition. Or at least I think so...
NS: Identifiers may be composed by any number of characters but keep in mind that not all of these characters will be significant.
2.3.Variables
A variable represents a memory location through the use of at least one identifier. In this memory location, we store binary data representing values of some specific data-type that is modified by the program as it runs.
NS: You must declare your variables before starting to use it. ¬¬ :P
2.3.1. Where Variables are to be Declared
Variables can be declared in three places, and each declaration place exerts something on some of the variables's patterns (i.e. lifespan).
variables are characterized accordingly to these places and patterns as follows:
- Variable declared as function parameter -- formal parameter
- Variable declared inside of a function -- local or automatic variable
- Variable declared outside of all functions -- global variable
These variables can be referenced only by statements that are both inside of the building block in which they are declared and that are written after the variable declaration.
A local variable has its lifespan determined by the time that its bluiding block takes to execute its task, which means that when the building block, who could be a function, finishes its job, the variable ceases to exist.
When the building block's work is required again the variable returns to its existence once again, but with no "memory" about what was the value that it was holding the last time it has existed (unless it's a static variable).
NS: In C, but not in C++, you MUST declare all local variables right at the beginning of its building block.
So, as stated before, a local variable cannot retain its contents between one call, that uses its building block and another unless they are declared using the static modifier as is show next:
I've considered, hypothetically, variable instantiation a life-death thing...I think that if I put things that way, everything becomes clearer...Ok, I confess it's kind of fun to do this thing too!! :P
Global variables are almost identical to static local variables, the difference resides in the access scope of these variables, global variables can be accessed by all the functions from the program, and local variables may be accessed only by statements put into the building block in which they are defined.
2.3.1.2. Formal Parameters
If a function is capable of receiving values of some type when it's called, we say that this function needs an argument, which are the value(s) passed through function calls. The local variables defined to store the argument of a function are know as the formal parameters of that function.
int Sum( int a, int b);
The formal parameters of the above function prototype are a and b, respectively.
NS: The integer values 10 and 3, that are passed through a function call as follows,
are known as Real Parameters. These values are used to generate the argument for the function.
NS: The integer values 10 and 3, that are passed through a function call as follows,
Sum( 10, 3 );
are known as Real Parameters. These values are used to generate the argument for the function.
2.3.1.3. Global Variables
When we declare a variable outside of all the functions, we are creating a global variable. Global variables can be used by any function of our program, even if they are defined in different files and it's lifespan corresponds to the program's execution lifespan.
NS: Global and static variables are both stored in a region of memory known as heap.
We usually declare a variable as global if it'll be used with a lot of frequency by our program.
NS: We must be careful, a large number of global variables in our program can lead us to some unwanted side-effects because we may lose control of when who receives, adds or/and increments who. :P
If in a function, or in any other building block, we declare a variable with the same name as a global variable, the building block in question will, by default, refer to it's local variable when the identifier, shared by both local and global variable, is used.
2.4. Access Modifiers
Access modifiers determine the way that a variable may be accessed. There are two of them present in the C language: const and volatile.
2.4.1. Variables Declared with the const Modifier
The values stored in those variables cannot be modified by the program, but an initial value can be assigned to them.
Ex:
- const int val1; // OK!
- const int val2 = 5; // OK!
- val1 = 3; // error!
NS: A hardware device outside of our program is able to change the contents of a const variable.
2.4.2. Variables Declared With The Volatile Modifier
Variables declared as this, explicitly tells that they may be modified not necessarilly by the ways specified in the program.
NS: We can combine const with volatile to explicit that the variable can be modified only by something outside of our program.
2.5. Storage Class Specifiers
In the C language there are actually four specifies : extern, register, auto, and static.
These specifiers state how the variable, in which they are used with, must be stored.
We use them at variable declarations as follows:
storage_specifier type variable_name;
2.5.1. The extern Specifier
The extern specifier "tells" to all the files that will compose the program about the existence of the global variable associated with it.
To understand this concept better, you must know the difference between variable declaration and variable definition, respectively.
The extern specifier "tells" to all the files that will compose the program about the existence of the global variable associated with it.
To understand this concept better, you must know the difference between variable declaration and variable definition, respectively.
- Variable Declaration -- Occurs when both type and name of a variable are specified.
- Variable Definition -- Occurs when memory is allocated for the variable.
float global_var;
in another file we can just declare the variable by making use of the extern specifier as follows:
extern float global_var;
2.5.2. The static Specifier
How to use it:
How to use it:
static data_type variable_name;
I've already talked about the use of the static specifier, but only on cases in which they're used within local variables declaration. Now, I'll explain the uses fo it within global variables declaration.
The difference between global variables declarations with or without the static specifier is that static global variables may be accessed only by the functions declared or defined at the same file as the respective variable. However, the access scope of non-static global variables is the entire program...always.
The difference between global variables declarations with or without the static specifier is that static global variables may be accessed only by the functions declared or defined at the same file as the respective variable. However, the access scope of non-static global variables is the entire program...always.