Syntax refers to the rules that specify how commands can be written in a language. C has several rules that must be followed in order for commands to be understood. They may seem picky, but if the rules are not properly followed, your C program will not be undestood and you will get compilation errors.
The first rule involves the use of the semicolon ";". Every command in C must end with a semi-colon. This was seen in the printf example:
printf("Hello world!\n");The semi-colon indicates the end of the command. If you leave out the semi-colon, whatever text appears next will be added to the command. Since this will normally not make sense, you will be given an error.
Control blocks are indicated using braces (curly brackets). A control block is started with an opening brace { and ended with a closing brace }. This syntax was used in the main function.
int main(int argc, char * argv[]) { /*the brace that starts the function*/ } /*the brace that ends the function*/You will also see braces used in other control structures such as loops and conditionals (if statements). These will be discussed in future lessons.
The arguments of a function are enclosed in brackets ( ). This was seen with printf where the argument "Hello World!\n" was enclosed in brackets. In defining the function main, the two arguments were placed in brackets after the word main.
A string is a list of characters, such as a word or sentence. Strings must start and end with quotation marks. "Hello World!\n" is a string. "hi23y1 323", and "word" are also strings.
Single characters are enlosed in single quotes. For example, 'a' is the character a.
Computer code quickly becomes complicated and difficult to read. If you are working on a large project, you may find that you forget exactly what you were trying to achieve with a particular command. Often several people will work on a piece of code. It is especially difficult to determine what code does if you didn't write it. Comments are a way of alleviating this problem. Comments allow you to add some description to the code, but the comments are not used to generate the final program. They simply make the code more readable.
In C, comments are started with the /* characters and ended with the */ characters. Anything between the start and end charactes is ignored by the compiler and will not effect the running of a program. Comments can be multiline or single line.
In C++, you can also use the characters // to indicate a comment. All text that appears after the // characters on a line will be ignored. // style comments are always single line comments. There are some example comments below:
/*This function is run first when the program is executed. It displays a simple message. This has been a multiline comment. It is a good idea to always add a comment at the start of a function that explains what the function does.*/ int main(int argc, char* argv[]) { //we are now inside the function - a useless one line comment /*This command displays the message - another one line comment*/ printf("Hello world!\n");//I could also put a comment here //Comments are only used to explain non-obvious commands //The return 0 command will have an obvious meaning to a //programmer and so doesn't need a multiline comment like this. //Note that the double slash comments can only be used in C++ return 0; }