Introduction

As we’ve learned, when programming, we basically provide a collection of instructions for computer to follow and execute. The programming language is designed to make those instructions easier to understand and remember, for us programmers, and provide flexibility to do many different things, also in different ways. Most importantly, programming language is designed to be consistent, once the program is written, there will never be ambiguity in how the program will behave. This is unlike the language we speak or write in, the same combination of words may mean totally different thing, depending on who says them when, and sometime in what tone.

C language is no exception, it has strict grammar that we must follow. Let’s look at the very first hello program we wrote,

C program instructions is organized in block called ‘function’,

function: is an execution block, the instructions in the function will be executed from start to end.

Function takes the following format,

<return_value_type> <function_name> ( parameters) { instructions; }

The <return_value> is something this function returns once it completes all its instructions, we’ll learn more on this later.

On line #3 above,  we can see that the return value is ‘int’, and the function name is ‘main’. Also this function takes two parameters, enclosed in a parenthesis ‘()’. we’ll explain what parameter means later.

Function then starts with a ‘{‘, with instructions in the middle, and ends with a ‘}’. At the end of each instruction, there must be a ‘;’.

White space or tab will be ignored, and can be used to format the code so it looks neat and easier to read. There are cases where space and tab are significant, we’ll learn all that later.

Within a function, we can call other functions, for example, we call the ‘printf’ function in line #4.

When programming we write our own functions, and also call to functions that are written by someone else, from a library. For the latter, we’ll need to include the library ‘header’.  In line #1, we include the standard I/O library header, since we use ‘printf’, a function from that library.

For the function we write, we can give it any name we like, as long as the name is allowed by C language grammar, for example, function name has to start with an alphabetical letter or _, and can not have space in between etc. There’s a few exceptions,

  • some name is reserved by C language, for example, you can not name a function as ‘int’
  • the same name can only be used once in one file

And there is a requirement for the very first function, it must be named as ‘main’, since this will be the main entry point to the program. No matter how big our program grows to, it always starts from this ‘main’ function.

Here’s another simple program, this time we write a new function to print out more info,

In above example, the ‘hey’ function takes no parameters, thus it has an empty ‘()’, and it has no return value either, thus the ‘void’ return type.

So ‘main’ function calls our ‘hey’ function twice, one after the other other, and in between we print a separator line.

Next, let’s look at more language basics …

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">