Basic C Structure

Ok, welcome to the first part of the C series! I'll just go over some basic stuff in C. First, I'll tell you that, if you want to practice any of the stuff you see here (and you're running Windows, that is) you're probably going to need an IDE or a compiler (preferably both). You can get a free one here... http://prdownloads.sourceforge.net/dev-cpp/devcpp-4.9.9.2_setup.exe?download and choose the closest mirror (I use Internap). Most of the examples that I write will work with dev-c++ (the compiler/IDE that you can download above), so if they don't with your compiler you might have to rename some of the includes. More on that later, though. First, I'd like to go into the basic structure of a C program, specifically the *.c file.

In C, there are these things called 'functions'. All of a C application/program goes into one, single, function, called 'main'. All of your code eventually ends up in the main function. The way a C program works is like this:

#preprocessor stuff
int main()
{
//variable declarations
int x;
int y;
//statements
x = 3;
y = x + 2;
return y;
}

Ok. What the heck was that??? It does, actually, make sense. Let's start at the beginning. The preprocessor stuff is what goes on before the actual program. Will be expained in more detail in a later tutorial. What's that int thing? In math class, you may have learned of integers. Integers are whole numbers that are positive or negative (or zero). An example of an integer would be 1, -23, 5 or 0. What would not be an integer would be .5, or -.0003. Integer is abbreviated to int, which is a datatype, or kind of variable in C. So, the int before the main means that the main function will return an integer. Make sense?

After the main we see a (). That shows that the main function takes no inputs from anywhere else. What inputs would the main function of our program need? More on that later. After the main(), we see a '{'. The curly brace shows where the function begins (and the other curly brace, the '}', shows where it ends, look to the bottom). These braces enclose most groups of information in C, so take note.

Variable declarations. Huh? What's a variable? A variable is just what it sounds like. Something that varies. The declaration means that we are basically saying "Here's a variable!" to the program. The // means a comment, something that the compiler ignores. Comments also can be done in the form /* comment */ for multi-line comments (yeah, the // only is for one line). Comments are helpful for saying what parts of a program do and for orginization. Anyway, back to the 'int x;'. Remember the int from before? It means; "Here is a variable, 'x' and it is of type integer." The declarations are necessary so that the program knows that you have a variable and how much memory to allocate for it. An integer takes up more memory than a- well, more on datatypes later.

Of great importance! The ';'. Come to know, love and appreciate it. The semi-colon comes after almost all C statements. Its so close to being all that you can (in most cases) get away with saying it comes after all C statements. If anything ever goes wrong with one of your programs, first check to make sure you have all of your semicolons. It is imperative. Ok. getting on with the program...

x = 3; In C, you initialize variables to have values. Also in C, when you have an '=' sign, it means that the variable on the left now equals the stuff (variables, numbers, etc.) on the right. This makes the x (a variable of type int) now equal to '3'. Make sense? Now on to the 'y = x + 2;'. This makes the y (undefined as of yet) equal to x (which was 3) + 2. The 'y' variable is now 5.

Now the return y;. The return goes along with the 'int main()'. If you set your function as '[variable type] [function]()', you have to have the function return that kind of variable. In this case, we set main to return an int, and so we return y, an integer. In most cases, when you have int main(), you return 0. Here I put return y; so that I could show you how return works.

Good job! If you read through that and understand (partially, at least) how C structure works, than great job! Now to a little more useful program/tutorial!