Basic Syntax


C has a strict syntax (typical of most, if not all, programming languages) that is necessary to be followed for a program to even compile. For this section of the tutorial, remember that a function is something like the entire int main(){(code here)} part of the example briefly described in the last chapter. Functions will be discussed in detail in a later section of the tutorial. Also, just know that preprocessor commands are commands that look like this:

New Lines

It might seem from the code sample shown in the last part of the tutorial that new lines are required in C's syntax. Creating new lines is actually usually purely for organizational purposes and omitting every single new line in a C program would not affect the program in any way. There is only one situation in C where a new line changes the ability of a program to compile or the function of the program and that is when a line of code is using the preprocessor (command that is #something, and is gray in Visual studio, like #include). The next section of code after a preprocessor command must be sparated by a new line or else the code will not compile. Multiple preprocessor commands in a row must also be separated by new lines. Anything that has punctuation after it(; , {) can be seperated by a new line (which is demonstrated in a code sample in the "Spaces" section.

This is what the code sample from the last part of the tutorial looked like:

That following code sample compiles and works in exactly the same way as the code sample above:


Spaces in C are required between key words and variable names. However, you can add as many spaces (after 1 space has already been typed) as you like between key words and variable names. Spaces aren't requried if the two pieces of code would be normally seperated by a new line (the semicolon, comma, or curly bracket then serves as the seperator).

That following code sample also compiles and works in exactly the same way as the code sample:

This code sample also compiles and works in exactly the same way as the code above:

Note that anything with some form of punctuation after it(; , {) can be seperated by a new line.

Eliminating new lines and spaces (or adding too many lines and spaces) makes the code harder to read. The process of making code harder to read is called code obfuscation.

Semicolons (;)

Semicolons in C are added at the end of a statement (what is usually a line of code). Semicolons tell the compiler where one command ends and another begins.

Curly Brackets ({})

Curly brackets in C are used to divide sections of code and create something known as a scope. Most of the time, this is not optional. Functions, for example, must always begin with acruly bracket, and end with a curly bracket. There are several other uses for curly brackets, but for now, you just need to know that a pair of curly brackets facing each other defines an aspect of the programming language that can be comprised of multiple statements (or lines of code, unless you decide to obfuscate your code like some of the examples above, in which case something that is typically multiple lines of code can be condensed into a single line). You can create a new scope anywhere within a function (must be inside of a function) by typing in a pair of curly brackets (what a scope does will be described in the "Naming" section below).


In C, names (such as "a" in the code examples above) can be comprised of lowercase letters, uppercase letters, numbers, and underscores(_). Numbers and underscores cannot be the first character of a name in any circumstance (unless, for some reason, your compiler allows this). Some compilers also allow you to use the dollar sign($) in naming your variable. Visual Studio, for example, allows you to use the dollar sign for naming you variables (even as the first letter). However, dollar signs are not part of the C Standard (there is actually a group that decides what every compiler should agree on), so beware if you plan on using compilers that do not allow this. Names within the same scope (curly brackets are used to declare scope, as stated in the section above) cannot have the same name as each other, or else the code does not compile. Scopes within other scopes (pairs of curly brackets within other pairs of curly brackets) cannot have variables with the same name as the scope surrounding them. Names can only be used in other parts of code, such as in printf("%i", 8);) in the same scope, or in a scope inside of that scope. Code that is not inside of curly braces is inside of a scope called the global scope, while code inside curly braces (such as code inside of functions) is inside of a scope called the local scope. Variable names cannot have the same name as keywords(such as char, int, and return) under any circumstances. You can name a variable "include", however, because it can be distinguished from the preprocessor command by the fact that it does not have a "#" before it. The same applies to other preprocessor commands.

This compiles (note that this code does not do anything except open a console window for a fraction of a second so small that you can't even see the window):

This does not compile.

There are three problems with this code. The first is that there are two variable "a"'s in the same scope. The second is that the variable "a" in the second scope is being set to a variable that was not declared in that scope (variable "b"). The third problem is that the function is trying to return something not declared in it's scope (variable "a"). I recommend that you try to run the code above and read the compiler errors in order to easily and quickly recognize those errors in the future in case you accidentally write code similar to the code above.

Other details about C syntax will gradually be explained as the tutorial progresses.