C Study Guide

St. Gabriel's College

|

Basic Syntax

The word “Syntax”

Syntax is a word we use to talk about “the correct way to write something”. If you write something incorrectly, you will have a “syntax error”. Here are some common pieces of syntax:

Keywords

There are some words that C considers to be "special". When you type them in a coding program, they will usually turn a different color. These words are called Keywords, they are words that are always used for one purpose. This is important, because you cannot use a Keyword as a name for a variable, or anything else. Here is a list of all the keywords in C:

auto _Bool break case char _Complex const continue default do
double else enum extern float for goto if _Imaginary inline
int long register restrict return short signed sizeof static struct
switch typedef union unsigned void volatile while

Including Libraries

You can use #include<> to get code from libraries by including their Header File (.h). These libraries may have functions that can help you. Here are some good ones:

You can also include your own header files, like this:

#include”mycode.h”

main()

Every program must have a main() function. When you run your program, the computer starts at the beginning of the main() function and stops at the return statement. A good way to write one is like this:

int main()
{
    return 0;
}

Comments

You can explain what a piece of code does using Comments. There are two ways to make comments:

//Single line comments like this

/* ...or you can make a Multi-Line Comment
            using slashes and stars like this */

Comments are removed before the code is compiled. They are useful for taking out a piece of code to see how your program will work without it.

//The following code is commented out. It will do nothing.

/* for(i=0; i &lt; 100; i++)
    printf(“This is the number %d.\n”, i);
*/