Sunday, September 12, 2010

C/C++ Part 3 - the sequeling

Last time I talked about Arrays and Loops. Next I need to mention conditional statements. The most common conditional is the if/else statement. The simplest way to show you how to use an if statement is to show you.

int x = 1;
  //Stuff goes here if true
} else {
  //Stuff goes here if false

Fairly strait forward. If the condition is true then the first block is processed. If it is not, and you have supplied an else, then the else is processed.

Up until now, as most have commented, C is similar to many newer languages like JavaScript. That is mostly due to the fact that they are based off of C. Now, I will talk about some features that separate it from the rest.

Pointers, for instance, are powerful and fairly easy to use. Pointers contain only the memory address of a variable. Creating a pointer is easy. Simply add an asterisk " * " in between the variable type, and its name. White space is not needed as the asterisk is a special character that cannot be part of a variable name. To create a pointer to an int:
int* Pntr1;
int *Pntr2;
int * Pntr3;

All three are correct. To "point" a pointer to a variable you simply need to use the ampersand " & " character before the name of the variable. Such as:
int Acct;
int* pAcct = &Acct;

And, finally, to treat the pointer as if it were the original variable, use the asterisk again before the pointer name. For instance:
int Acct;
int* pAcct = &Acct;
*pAcct = 5;

In this example Acct will be set to 5 and the pointer will remain unchanged. In C the most common use of pointers if for whats called a "C-Style String" which is simply a pointer of the type "char". Strings, of any type, are able to store lines of text as an array of chars terminated by a " \0 " (a special character with a value of 0).

To use strings, and other special types, you will next need to know about the #include statement. #include is a pre-processor directive meaning that it is processed during the compile process (when the compiler turns the code into a program). Pre-processor commands are all independent of the program you are making and, in fact, will not be part of the final product. There are only a few commands and the ones you will most need are:

#include - includes a file.
#define - creates a pre-processor variable used both in #if statements and in the C code
#if - operates mostly the same as a normal if statement
#ifdef - if a pre-processor variable is defined, then the following code will be compiled.
#ifndef - if a pre-processor variable is NOT defined, then the following code will be compiled.
#else - if the last #if command was false then this code is used instead.
#elif - same as an #else except it also includes its own condition.
#endif - ends an #if statement's reach.

The rest can be found here as well as some other interesting info. I am not going to go into detail about C include files but most of them can be found here.

Next time I am going to talk about C++ which adds many new things.

1 comment: