March 24, 2018

Structure of C Programs


   A C program is a collection of one or more functions, where a function is defined as a group of C statements that are executed together. 



General structure of C program

Comments
Preprocessor directives
Global variables
int main(void)
{
    local variables
    statements
    ----------
    ----------
    return 0;
}

Function1()
{

    local variables
    statements
  
    ----------
    ----------
}

Function2()
{

    local variables
    statements
  
    ----------
    ----------
}
----------
----------
FunctionN()
{
   

    local variables
    statements
  
    ----------
    ----------

}



Comments

Comments can be placed anywhere in a program and are enclosed between the delimiters /* and */. Comments are generally used for documentation purposes.
 
Preprocessor directives

Preprocessor directives are processed through preprocessor before the C source code passes through compiler. The commonly used preprocessor directives are #include and #define. #include is used for including header files. #define is used to define symbolic constants and macros.
 
Header file

A header file is a file containing C declarations and macro definitions to be shared between several source files. Header file has .h(dot h) extension. The header file is included in program with the preprocessing directive #include.  There are two types of header files: the files that the programmer writes and standard header files that comes with your compiler.

Including a header file produces the same results as copying the header file into each source file that needs it. Such copying would be time-consuming and error-prone. So it is not a good idea to copy the content of a header file in the source files, especially if we have multiple source files in a program.

To use any of the standard functions, the appropriate header file must be included. This is done at the beginning of the C source file.

For example, to use the printf() function in a program, which is used to display information required by the user and also prints the values of the variables, the line #include <stdio.h> is required because the header file stdio.h contains the printf() function.

Function

Every C program has one or more functions. If a program has only one function then it must be main(). Execution of every C program starts with main() function. int written before the main() function is the return type of main() function. The curly braces { } just after the main() function encloses the body of main() function. main() function has two parts, declaration of local variables and statements. Statements in the main() function are executed one by one. The statement return 0; at the end of main() means that the function has terminated successfully. Other functions are user-defined functions, which also have local variables and C statements. They can be defined before or after main()
 
Global & Local variables

There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions. Global variables are automatically initialized to 0 at the time of declaration. The global declaration section also declares all the user-defined functions, structure, union etc. 

The local variables are defined within the body of a function or a block are local to that function or block only. These variables are created when function is entered and are destroyed when the function is exited.


First program in C


/* my first program in C */
 
#include <stdio.h>

int main()
{
    printf("Hello, World ! \n");
    return 0;
}



Let us look various parts of the above program:

  • The first line of the program /*…*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program.
  • The next line of the program #include <stdio.h> is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation.
  • The next line int main() is the main function where program execution begins.
  • The next line printf(...) is another function available in C which causes the message "Hello, World !" to be displayed on the screen.
  • The next line return 0; terminates main() function and returns the value 0.




No comments:

Post a Comment