Create your own game
eng   рус

C Language Basic Syntax

Previous tutorial: C Language Hello World
Next tutorial: Data types and variables in C language

In this tutorial we'll learn basic syntax (rules) of C programming language. It's one of two tutorials where there will no be exercises.

Sequential execution of programs in C language

Code in C (and in any other programming language) is executed from top to bottom. Let's look at the example:

printf("Hello!\n"); printf("Bye!\n");

If you run the program which contains this code, you'll first see in the console "Hello!" and on next string "Bye!". Soon we'll learn how to execute specific block of code and how to execute the same code several times - there are special constructions for this in C.

Comments in C

Very often we need to have in the source code comment that will not be executed and which purpose give a hint to other coders: what exactly your code is doing here. There are two variants of comments in C:

/* Multi-string comment. All text between /* and */ will be ignored by processor. */ printf("Hello!\n"); // double slash is used to mark one-line comment printf("Bye!\n"); // Such comment will end at the end of current string.

Easy, right?


C defines very limited set of keywords - reserved words that have special meaning in C and you can't use them for other purposes. They are:

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

Despite the C language was created in 1972 some of this keywords were added only in 2011.

Many of this keywords we'll use a lot (if, while, for, int...) but one keyword I don't recommend to use ever. It's goto. goto allows to change execution order: it tells processor go to any line of code and continue from it. If you'll use it in your code, other coders (not so kind guys like me) may beat you for it... maybe to death. There are better capabilities in C to change the execution order of commands.

Formatting the code and indentation in C language

C language allows free formatting. It means you can use as many white-spaces as you want. These are the same:

printf ( "Hello!" ); print("Hello!");

In "Hello, World" program we have seen the main function:

int main(void) { printf("Hello, World!\n"); return 0; }

Here code inside braces is indented by two white-spaces. It's not necessary but it's a good manner to make indentation in block of code (we'll check what it is below).

Usually it's 4 white-spaces or a tab. As for me I prefer 2 white-spaces and all code in following tutorials will use it.

Blocks of code in C language

We can create blocks of code by putting code in braces { }. So body of the main function is a block of code. In C blocks of code defines scope - that's very important and quite tangled in the beginning idea, so we'll talk about it in more details later.

Statements and operators in C

Source code in C mostly consists of statements. Statement is an 'action'. You tell the process to make something: print string, sum two numbers, allocate memory... Example of statement:

printf("Hello, World\n");

Each statement must ends with semicolon. That's common mistake to forget to put semicolon. Statements consist of operators. Operator is a symbol that defines specific operation like multiplication or division. Operators have precedence order - it defines what operator should be evaluated first if two different operators are in one expression:

2 + 2 * 3 + 6 / 2; // * - multiplication, / - division in C

Here first evaluated 2*3, then 6/2, then left sum, then right sum. All expression is evaluated to 11. All expressions in C language (doesn't matter how complex they are) are evaluated to some value - result of statement.


It's quite easy stuff. Only thing I want you to remember (and you probable didn't pay attention to it): all operators are evaluated to some value (and it means all statements too).

That's it for today


No comments yet