A Tutorial of C and C++



Table of contents
Overview
The Basics
Declaring Variables
Declaring Functions

Overview
For the most part, C and C++ are very similar. C++ has everything that C has plus a few additional features that make it a bit easier to use. Also, C++ has support for Objects (classes) and this makes it an Object Oriented programming language. But for now, lets get started with the basics of C.


The Basics

Declaring Variables
The first place to start is to know how to declare variables. Declaration of variables is the same in C and C++ and the basic syntax is:

Type Name;

Where Type is the type of the variable and Name is the name of the variable. This is the name that you will be using in the program. The Type can be one of:
So, if I wanted to declare an integer and call it 'size', I would say:

int size;

Now I have an integer called size which I can use in my program and I can give it a value and add it to other integers. Here is an example:

int a;
int b;
int c;
a = 7;
b = 5;
c = a + b;

Here I declared three integers called a, b, and c. Then I set the value of a to be 7 and the value of b to 5. Finally I add a and b and put the result in c.

This is all you need to know about declaring variables that it just goes Type and then the Name. So if I had a type called 'Foo' and I wanted a variable of type 'Foo' called 'Fred' I would say:

Foo Fred;

Variables are used all the time in C and C++, it is very important that you know how to declare variables and use them. You should now be ready to move on to the next topic which is how to declare functions and procedures.

Declaring Functions
Funtions are the second most important topic in learning C and C++ because every line of the program is inside a function. Every C and C++ program must have at least one function and that function is called 'main'. This is where all C and C++ programs start. By now you're probably wondering, 'well, what does a function DO?'. The answer to that is easy. A function is a chuck of a program that we give a name to. When we use a function we say we are 'calling' the function and when we call a function it simply executes all the lines that are in the function. Functions are completely self-contained meaning that if you declare a variable in one function, then no other function can use that variable, but other functions can declare a variable with the same name, but they are NOT the same variable. Well, it kind of seems like functions are pretty worthless since they don't know the value of any variables outside if it's body, that's why functions can take what we call 'parameters' and these are just variables that we give to the function. However, the function can give whatever name it wants to it's parameters. But before we look at 'main', lets look at how to declare a function in general and you will see that it looks very simlar to declaring variables:

Type Name([parameter list])
{
[Function body]
}

At first glance it may not look at all like a variable declaration, but the only difference really is the parameter list and the Function body. The parameter list is a list of parameters that are passed into the function and they look like a long variable declaration list. The Function body is where all the code for this function goes. One example of a function is the Square function. This function takes an integer and returns the square of that number, which is simply that number multiplied by itself so the function for that looks like:

int sqr(int num)
{
return (num * num);
}

1