Thursday, January 24, 2008

The C Programming Language - Expressions (Under Construction)

Basically, expressions are composed by data and operators. Data can be represented by variables or constants, and the operators acts upon the data to generate a result that comes from some kind of data manipulation.

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
2.3.1.1. Local or Automatic Variables

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,

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.

    • Variable Declaration -- Occurs when both type and name of a variable are specified.
    • Variable Definition -- Occurs when memory is allocated for the variable.
For example, if in a file we declare and define a variables as follows:

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:

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.






Samuel

Wednesday, January 16, 2008

The C Programming Language – A Overview of C

1.1. C as a Midle-Level Language

According to Schildt’s definition, C is classified as a middle-level language because it combines the best aspect of high-level languages with the control and flexibility of a low-level language (ex: Assembler). The C language allows manipulation of bits, bytes, and adresses. C programs in most of the time are also well know for having great portability, which means that it’s easy to convert programs written in C from one platform to another.

C is also well know for not performing much of type checking and run time error checking routines, which means that the programmer is responsible for that :P.

1.2. C as a Structured Language

One of the main characteristics of implementations build upon a structured language as C resides in the application of a concept called compartmentalization. Generally in compartmentalization we section off and hide parts of the program that are responsible for the execution of specific tasks. A good way to compartmentalize code in C is through the use of building blocks (logically connected groups of program statements between brackets ({ })) more specifically through the use of functions. I'll explain the function concepts in a different post. For now, think about functions as a special nested group of building blocks that is called (or summoned :P) to do something in the battlefi...I mean...to do something during the program execution. :)

Compartmentalization allows the use of local variables. This type of variables are defined and are meant to exists only in the building block in which they are declared, with this we can prevent some side-effects that can be really a pain.

NoteStorm: Side effect is the result of the access and alteration of some data by a specific task in which the data modified in the execution of process has nothing to do with the purpose of the task.

C’s functions are building blocks in which the main activity related to program occurs and as a matter of fact it can be considered the main structured component present in the language. With the use of such structures we can declare local variables

NoteStorm: C don’t allow definition or declaration of functions inside of other functions whatsoever. So, because of that we can say that C is a structured but not technically a block-structured language.

1.3. C as a Programming Language

As a programming language, we could say that C has few restrictions, few complaints, block structures, stand-alone functions, and a compact set of keywords and if we combine that with some good programming techniques we can achieve the perfomance that we would achieve by the use of some low-level language like Assembler combined with portability and the structure of ALGOL or Modula-2.

1.4. The Form of a C Program

All C programs consists of one or more functions and an undetermined number of function calls. The function that must exist no matter the case in a C program must be denominated as “main”. The main function is the first function that’s called during the execution of a C program. And normally it has, in between its brackets, only function calls. Those calls can give us an outline of the program.

Example:


1.5. The Library and Linking

There are a set of functions (a library) that every C compiler must have in order to meet the standard specifications defined for the C language.

You can use the functions from the standard function library from C in your program through the use of a process know as
linking. The linker combines our code with the code from the standard library.

NS: The C++ language inherits the standard library from the C language.

Both C and C++ allow us to separate our program in many files. During the linkage process, the program code contained in all the files is linked along with the library routines's code to form the object code.

1.6. c and cpp Extensions

Briefly speaking, .c and .cpp are the extensions of files that contains C and C++ programs respectively. It's important to say, right now, that there are some and sometimes great differences between programs written in C and programs written using C++. Those differences will become clear as we advance through the tutorials.

I would gladly accept any opinion about the subject, cuzz clearly I'm not an expert in it, neither in english, so if anyone that is more experienced in both areas of knowledge want to share some information please don't hold yourself. >:)



Samuel

Tuesday, January 15, 2008

Part I - Introduction to C

First of all, I'll give a brief introduction to the C language. Denis Ritchie is the creator of this language.
This introduction will be entirely based upon the book C++ The Complete Reference from Herbert Schildt. It's a great book, if you have the oportunity go and grab it... most of the time I'll try to apply what I'm talking to the game development area.



Summarizing, in this section I'll talk about the next topics:
  • The C subset of C++;
    • A overview of C;
    • Expressions;
    • Statements;
    • Arrays and Strings;
    • Pointer;
    • Functions;
    • Structures, Unions, Enumerations, and User Defined Types.
    • C Console I/O System;
    • C File I/O System;
    • Preprocessor Directives and Comments.
It'll be quite a long jorney don't you think?

So let's begin!!

Samuel

Yeah!! That's me trying to learn C and C++...and English... :P

Well,
My name is Samuel Antonio Branco Coelho and this blog was created for me in an attempt to fixate in my mind what I've been studying about C and C++ and to help other people who want to learn C and C++ out in the world.
I'll try the best that I can to release some good and well designed material, but I must say that I'm not very familiar with english...so, I think you could expect some not so well constructed phrases and some nonsenses sometimes... :P

I'll base my posts here mainly upon two books: C++ The Complete Reference and C++ Professional. My main idea is to create posts that contains a review (or summary) of the subject talked about in three formats: text and video or audio (this one here will be fun...be prepared to laugh from my english :P) After the reviews I'll try to post some exercises too...

I have to thanks a lot some people for the encouragement that was gave to me by them... Its thanks to them that I've lost my fear from do what I was feeling that was the right thing to do. Those people are: Davi Coelho (my older brother), Marcia and Sandra Coelho, my parents, Rafael Rocha and Thayane Rocha (both my cousins), the Alencar brothers Daniel, Danilo and Bruno (great friends), Edison Garreta and Ricardo Daltro (both great friends of mine too) and Cory Barlog (A really great developer that I was able to talk to a little), . Guys, it was thanks for your help that I was able to get up and try again many times when my dreams were seem to be slipping through my hands... I really don't know how to thank you all people!! :) I must also send my gratitude to God, for puting all those wonderful people in the path of my life!!

Well, that's it. I do hope that whoever take a look at this blog can be able to find something useful and even fun in it!! That's it. :)

Samuel