- Give up programming. This is unacceptable to the majority of people who wish to progress into the industry.
- Move to a new language.
Far be it for me to ignore a long time computer tradition therfore the first program I will use to explain C is the immortal "Hello World!".
/* Hello World */
#include <stdio.h>
main()
{
printf("Hello World!\n");
}
Right, we'll go through this 1 line at a time. The first line is a comment. In C comments start with /* and end with */. Everything inside these is ignored and they can be put just about anywhere in your code. The next line is a bit more interesting. Any line which starts with # is interpreted by the pre-processor before the program is compiled. This one, #include, is probably the most important of it's commands; it looks for the file with the name that is enclosed in the angled brackets and inserts it in that position in your source file. This file,stdio.h, is the file that contains all the descriptions of the console and file I/O functions. You must include the header files(those ending in .h) which contain the functions you are using in that program or it won't compile correctly.
The next part of the program main() defines the start of a function. A function in C is much the same as a procedure in BASIC and is defined by the name of the function followed by brackets round any parameters it may have. The actual commands in the function are then enclosed in curly brackets({}). The main() function is the only one that every program must have. It is executed first and when the end of it is reached the program ends. This leaves only one line left printf("Hello World\n"); The printf function is one of C's most powerful output functions as you will see later. Here the text inside the quotes is printed on the screen. The \n pair is a special combination in C strings and it means take a new line. The semi-colon at the end of the line tells C that you are calling a function from here to be executed instead of defining one.
There, you can program C. Now we will slow things down a bit and I will go through all the common C constructs and their BASIC companions.
If |
if(a == b)
{
a++;
if(a == 9)
printf("a equals 9");
}
else
{
a--;
b = b * 2;
}
| If a=b
a=a+1
If a=9
print "a equals 9"
End If
Else
a=a-1
b=b*2
End If
|
| C version | BASIC version |
This guide is still under construction and will be gradually updated as I write each part but it WILL be finished eventually.
Disclaimer