A simple C++-program:
#include <stdio.h>
void main()
{
printf("Hello world!\n");
}
And now, compile the program and start it! (See into your compiler-information how to do that).
You will see the following screen:
Hello world!
<--- This is the cursor, if any
_
NOTE: If you use Borland C++, press Alt-F5 to see the output screen.
OK, let's see what happend:
#include <stdio.h> |
This line tells the computer to include another program to ours. |
void main(); |
Here begins the main program. Later you will see what that means. |
{ |
The { }'s mark the boundaries of the main program. |
printf("Hello world!\n"); |
This line tells the computer to print "Hello world" to the screen. |
} |
See above. |
The ;'s must be specifyed after some commands; you must learn where to use them simply by trial&error.
The #include refers to a program where printf is defined.
printf is a command: It tells the computer to do something, in this case it tells to print anything to the screen.
Characters in ""'s are named string. Note the "\n" in "Hello world.\n". This is the replacement of the return&line feed chars. The computer doesn't do this automaticially with printf. Here is the list of all possible replacments:
\a | bell |
\b | backspace |
\f | line feed |
\n | return&line feed |
\r | return |
\t | tabulator |
\v | vertical tabulator |
\\ | \ |
\? | question mark |
\' | ' |
\" | " |
\ooo | Any char in octal |
\xhh | Any char in hex |
/* ... */: everything in this delimiters is ignored. With them you can write remarks into your programs. Strongly recommended!
(Note: at the end of each chapter I'll give you some tasks to do.)