Basic Syntax

Like English, C++ has words, space and punctuation. Also like English the number of spaces between words, or the position of line breaks (as long as they don't break a word in half) do not affect the meaning of a sentence. The compiler does not care how easy it is to read a piece of source code. Using spaces, line breaks, and indentation is for the convenience of human readers (such as yourself). However, computers are sticklers for 'proper' spelling, punctuation and, in the case of C++, capitalization. If you leave out a comma or a semi-colon, or use a lower-case letter where an upper case one should be used it will change the meaning of the code, and the compiler will probably complain. (Actually it's worse when the compiler doesn't complain, because you end up with a program that doesn't do what you wanted!)

Of course, C++ is not English. The punctuation, the use of capitalization, the words and their meanings are all different.

Tokens

In C++ a word or a single piece of punctuation is called a token. Tokens are separated by white space, which can mean any number of ordinary spaces, tab characters, or line breaks. Punctuation, like parentheses '(' ')', braces '{' '}', or commas and periods, don't need to be separated from other tokens by spaces. For example the tokens in this piece of code:

int main(int argc, char* argv[])
{
    return 0;
}

Are the following, in order:

int
main
(
int
argc
,
char
*
argv
[
]
)
{
return
0
;
}

Written either way the code has the same meaning, but obviously the first one is easier to read.

Keywords

In C++ there are a relatively small set of words that have fixed meanings. These words are called keywords and they include:

Plus some others. Learning C++ is not a matter of memorizing a bunch of keywords, but it is good to be aware of reserved words like these, so that you don't try to use them as variable or function names.

Symbols

Words that are not keywords are available for use as the names of types (including classes, structures and enums), functions, variables, namespaces and such programmer-defined objects. Symbols, as such words are called, can generally consist of any combination of letters (upper-case and/or lower case), numbers and the underscore character '_'. One exception is that you can't begin a symbol name with a number. The following are all valid symbols:

x
name
nCount
caBuffer
A_long_variable_name
ReadFile
m4
_danger

The last example, that begins with an underscore, is a valid symbol. However, you should generally avoid such symbols since symbols with leading underscores are often used by libraries to avoid name conflicts (a better method is to use namespaces, but namespaces are a recent addition to C++).

Comments

In order to make your source code easier to read and understand it is useful (absolutely necessary in fact) to use comments in plain English (or whatever your native language might be) in the code to explain and clarify. Consider the difference comments make between:

    WNDCLASSEX wc;

    memset(&wc, 0, sizeof(WNDCLASSEX));
    wc.size = sizeof(WNDCLASSEX);

and

    WNDCLASSEX wc;  /* Window class structure */

    /* Prepare the window class structure to be filled in */
    memset(&wc, 0, sizeof(WNDCLASSEX)); /* Zero clear */
    wc.size = sizeof(WNDCLASSEX);       /* size is the size of the structure in bytes */

There are two ways to include comments in your code. A comment can be enclosed in /* */, like the above. This type of comment can cover several lines or have ordinary C++ code on either side on the same line:

    /* This is a 
       multi-line comment */

    int main (int argc /* Argument count */, char* argv[] /* Array of arguments */)

The second way to include comments is to put two slashes (//) before a comment. After the two slashes everything up to the end of the line is ignored by the compiler.

    // This is a single line comment.

    int x;   // A variable declaration.