Synfire's Guide To C Programming

1. Basic Structure

All programming languages have a structure that keeps things looking neat. Languages such as PERL have abandoned this concept, but my personal opinion is that to create good, easy to modify source you should have some form of predetermined structure. C's structure is:

'comments'->'include'->'define'->'initiate'->'main()'->'functions'

This may sound weird but you will find it to be very simple in after you finish the next few sections. Now lets look at an example code...

simple.c
#include <stdio.h>

int main()
{
    return(0);
}
Now this program will compile and run, but it don't really do anything except serve my purpose very well :)

On the first line '#include <stdio.h>' stdio.h is a HEADER FILE (a header file is a file that holds code to simplify programming). Basically, #include places the contents of stdio.h into the source as though you typed it in yourself. (stdio stands for STanDard Input / Output)

On line three, 'int main()' is a special function that is automatically called when the program is executed. Anything between the parenthesis on the next line is executed first. But let me break line three down, the first part is the return type, when the program ends you must 'return()' an int (discussed in greater detail in section 2 & 6). If you don't want to 'return()' anything then you would use 'void main()'.

Now on lines four through six, you have the CODE BLOCK (a code block is a group of statements within parenthesis. Note line five, the semi-colon at the end of the statement. After every statement is a semi-colon. In the next code snippet Is the same thing except you will see comments being used. Comments allow you to place notes in your source code to make editing easier.

Anywhere you wanna put a footnote you just have to add it in.

simplecommented.c
/* This is a simple non-useful program */
#include <stdio.h>

int main()
{
    return(0); /* anything between these is overlooked */
}
Now this is as far as I'm gonna go into the structure of C, mostly cause it's 4:00am, but as you move on you will see the structure of C become much more clear.

< < < Introduction | Lesson 2: Standard I/O > > >



Concerned about phishing or want to know more about it? Then read our fake logins page.