The C and C++ programming languages are among the most popular and widely used languages available. C++ is an extension to the C language, allowing Object Oriented Programming. The C language is used in the lab for programming PIC chips, and you may wish to use the extra power of C++ for programming more complex things such as AI.
Rather than a full introduction to the C language, this paper simply provides you some notes on the basics.
1 Statements
A program is made of a number of statements. In C, all statements must end with a semicolon (;). C ignores whitespace, meaning you can put spaces, tabs and new lines in statements without altering the meaning. Statements may be grouped together in blocks by wrapping them in braces: { and }. These have a similar function to begin and end in pascal.
Note that the C language is case sensitive, meaning ‘X’ is different to ‘x’.
2 Comments
There are two ways of adding comments to a source file: A single line comment is started with //, or a multi line comment is wrapped in /* and */. Note that // is strictly C++, but many C compilers support it. Examples:
int x; //Comment here explaining what x is…
int w;
/* long comment here,
it’s on multiple lines, so we use the other comment style
*/
It is very important to put comments in your files! Explain what you are doing and why in comments, as this will make it much easier to debug when something doesn’t work. It also makes it possible to edit your code a long time after you wrote it, or for someone else to edit your code.
Friday, September 25, 2009
Subscribe to:
Posts (Atom)