C101: program structure

C program consist of functions and data known as variables. All c program must at least have one function. By default this function is known as the main function.

When C programs is started, it begin its execution from this function. Simply by renaming this function by placing an underscore like _main() will produce syntax error when you compile the program.

All c program cannot have more than one main() function. This causes confusion for the compiler, when it determining which one is the real starting point for the program.

C program has keywords that has its own meaning. It has certain words such as return, int, include, while and etc. For example include keyword will include the header file into the program and int keyword declares a variable as integer.

Variable name, function name and other identifiers are case sensitive, meaning two exact words written in different case is different, for example these two variables num and Num are different for the c program.

Following is a layout of the basic C program

#include <stdio.h>

// global variable declaration
int globalVariable;

int main(){
  // statements
  return 0;
}

The first line in called pre-processor include, it includes the stdio.h file into the c program. the stdio.h is a header file that contains input and output functions such as printf() and scanf(). without including this file, this program cannot use them, if try to use them compiler will recognise or understand what are you trying to say.

Second line is a global variable where this kind of variable can be used in the entire program, as oppose to local variable. Local variables are declared inside of a function, and can only be used within that function.

Next is int main(), where the main function begins. this line declares the functions name and what type of data it returns. Oh yeah most of the function returns some value when function finish its task. even when the function do not return value, we still need to specify return type which in this case we write void. But for the main function its always int.

Then the body of the function that contains all the statements belong to this function which all of the statements will be executed when the function is called. We write them in between opening curly { and closing curly }. This is also called code block. A function may not have any statement or there is basically no limit for the number of statement.

Last statement in the main function is return 0. it returns the exit status index.

//function prototype

int main{

}

dataType functionName(parameters){
  //statement(s) including return statement;

}
...

Finally, we can add as many functions we want in the program. What we are looking at is the syntax of the function declaration. data type as we discussed earlier, can be void, int, float, boolean, etc.

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net