27 October 2013

Basic Structure of C Programs

Every programming language has its basic structure, that is, how and where the lines of code are written.

In C, it looks like this :
#include <stdio.h>

int main(){
    //statement1;
    //statement2;
    //..........;

return 0;
}
Let's start from the top.

The #include statement is used for including a header file into your program. In this case, stdio.h.
It must precede any other lines of code in your source file.
The int is a return type specifier for the function main(). Every C program must have a main() function in order to run. Even though your program has a thousand lines of code, it will start by executing the main() function.

More about return type of main():
The main() function returns 0. which is an int as an indicator to the compiler that the program was well-executed (without error).

Then, there are these symbols, the {parentheses}, to indicate the start and the end of the main() function. Every line of code, or statement, within the parentheses must be followed by a semicolon (;).

Finally, there is the return statement, which pretty much describes its function already : to return a value from the function it represents. In this case, it is the main() function.

The End. By now you should have the basic knowledge to start writing codes in C.



Happy Coding,
Cyber Frost

No comments:

Post a Comment

Search