Sage-Code Laboratory
index<--

C Syntax

C is a low-level imperative and structured programming language. We say it is a compiled language because we must run a compiler to convert source code into machine instructions. C require minimal run-time support and produce native executable code. That can be run on a specific platform. It is also a strongly typed language. It is not object oriented nor functional programming language though it has functions and not procedures.

Punctuation

All source code is contained in subroutines. Each subroutine has a name and is enclosed in a block of code. C language uses curly brackets to start and end a block of code. One block of code can contain numerous statements. Each statement is ending with semi-column: ";".

C is a free form language. That means spaces and tabs are ignored. However many times one space is required as separator. A good practice is to use code indentation at equal number of spaces for nested blocks of code.

C is case sensitive language. There is a small, fixed number of keywords used to represent actions or declarations. These are reserved keywords you can not use as identifiers.

Keywords:

The only way to learn something is to memorize. You can memorize in two ways: The hard way and the easy way. The hard way is to understand first then memorize. The easy way is to memorize first then understand. Both are good. Next list must be memorized but just read it twice. These are reserved words in C.

auto break case char const
continuedefault do double else
enum extern float for goto
registerinline int long if
restrictreturn short signed sizeof
static struct switchtypedef union
unsignedvoid while volatile 

Note: Not all C keywords are English words. For example: enum, extern, typedef and goto are invented words. Therefore sometimes a text editor that do not know C will wrongly underline these words with a red line as incorrect. You can ignore this if you are positive you have spelled correctly the keyword.

Comments

To document your code or have some code commented out you can use block comments using delimiters:  "/* … */" and the line comments using delimiter "//". A line comment can be used at the beginning of a new line or after statement, before the end of line.

Example:

//processor directive
#include <stdio.h>

/* Any C program must contain a routine called main()
   This routine is actually a function that return one integer result: "0"
*/
int main()
{
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0; // normal termination
}

Execution:

Input

Data input can be done using scanf() a function similar to printf(). For reading data, first you print a message using printf() then accept the user input using scanf().

Example:

#include <stdio.h>
int main()
{
    int test;
    printf("Enter number: ");
    scanf("%d", &test);
    printf("Number = %d",test);
    return 0;
}

Output

You must already get it from example. The main function capable to print output to the console is printf(). This function accept a string template as first parameter and optional a second parameter representing data to be inserted into the template.  Character "%" is the placeholder for data element that is different for each data type.

Example:

#include <stdio.h>
int main()
{
    char chr = 'a';
    printf("character = %c.", chr);
    return 0;
} 

Exit status

Unlike other languages C do not have statement for error handling like try or trial  or anything like that. The only thing we can do is to return an error code from a function. Then you can analyze this code.

Main function is usually returning "0" = zero if the program finished correctly and "-1", NULL or a different code that indicate an error. We will have an article about error handling at the end of this tutorial. Until then this is all you need to know to understand the examples and practice with the main function.


Read next: Data Types