Your first tutorial!
A c program consists of functions, each function
is a block of code which can be executed over
and over again. The compiler looks for a special
starting function when compiling your program,
this function is called "main". Every function
is enclosed by curly braces {}.your first "main"
should look like this:
void main( void )
{
}
So, what do the words 'void' mean? The first
'void' is what type of data the function will
return, the second 'void' is what type of data
the function will accept. Both of these will
be explained later, all you need to know now
is void means "no data". Essentially we're
telling the compiler this function will accept
and return no data.
Our next function is called printf. It has
already been written for us and allows us to
print text to the output window. This function
exists in a file called stdio.h (Standard Output).
The first thing to do in our program is to tell
the compiler we might want to use any functions
in stdio.h
//#include is a preprocesser directive
//in layspeak, it tells the compiler to do
//something before compiling our code
//in this case we're telling it to include
//all the code written in the file stdio.h
#include <stdio.h>
//now we start our program
void main( void )
{
printf( "Hello World!" );
}
That's it, your first program! Remember when I
said a function can accept data? printf accepts
data as an array of characters (called a string)
we pass it this data inside of paranthesis...which
is our "Hello World".
If you're using Windows and not running in Visual
Studio, the program probably runs and disappears too
fast. In the next tutorial we'll show you how to
pause the screen.But for now you'll have to run it
from a DOS prompt or run it inside the Visual Studio
Debugger (the play button in Visual Studio).
|