The C program Structure

    A program is set of instructions given to a Computer. Compiler translates the Programs written by us to a language that Machines can understand, because it's obvious computers can't understand natural languages. A set of formal rules are defined for every programming language, if any of these rules are not met by your program, the compiler raises an error. These errors are called 'Bugs' and the process of rectifying these errors is called 'Debugging' (Sounds professional, right?).
    
    Every C program has to comply with these rules, if you want your program to do what it is intended to do. A very basic C program can have the following structure:

  • Header files & Macros inclusion area.
  • User defined Functions/Forward declarations/Global declarations area.
  • A main function.
Most of the above may not make any sense to you if you are new to programming. We will look at it step by step.

Header files:
    These are set of functions, macros, variables provided by the creators of C that help you develop your programs faster. You need to tell the compiler that you are making use of a header file when using each of it's properties(variables, functions), otherwise the compiler will not be able to understand what they are, and as expected, raises an error. 

Main function:
    It is a function where execution of the program starts to take place. 

Let us look at a simple program that will make you understand the program components.


#include<stdio.h> //standard Input/Output Header file
int main() //main function with return type 'int'
{
    printf("Welcome to Do it in C!"); //function for displaying output, offered by stdio.h
    return 0;
}


The first line

#include<stdio.h>

tells the compiler to include header file stdio.h provided by the C library, which contains many ready made functions. You will come across many other header files in future.

//standard Input/Output Header file

This line is a comment. Comments are ignored by compilers. They are useful to explain what a specific block of code does, like I did. You can write absolutely anything in comments and the compiler doesn't care.
Single line Comments start with //
Multi line comments start with /* and end with */


int main() //main function with return type 'int'
{
    printf("Welcome to Do it in C!"); //function for displaying output, offered by stdio.h
    return 0;
}

main() is a function. Every function has the following structure.

  returnType  functionName (Parameter list)
  {
      ....
  }

    return type determines what type of values a function can return to the caller. In this case, it returns int, short for integer. It is a fundamental datatype offered by C. You will learn about datatypes shortly.

    A list of values can be sent to a function and 'Parameter list' identifies them and uses them, if needed.

    Every function definition starts with a { and ends with a }. So what ever you write inside the curly braces for a function, belongs to that specific function. you will also learn what happens if you try to access some data belonging to a function outside that function.


printf("Welcome to Do it in C!");
    This is a function call. We call other functions by the following syntax:

    returnType  functionName (Parameter list);

    If you examine carefully, "Welcome to Do it in C!" is a parameter sent to a predefined function 'printf'
Every function call, and most of the program instructions, end with a semi colon ';'

return 0;

      Recall that return types determine which type of value is to be returned by a function. In this case, the main function is returning a value 0 of type 'integer'. Generally, this means that the main function has completed execution successfully without any errors, and returned the control to the operating system for further tasks.



    So, here ends a basic program in C. We will go into deeper details from the next lesson. Try doing some more simple programs. Practice makes a person perfect , for sure (the 10,000 hour rule!)
Comment below for any questions, we are happy to help!




Previous
Next Post »