Wednesday, 20 March 2013

Introduction to C Programming - Part II

Welcome back to series of articles on Introduction to C Programming. In this article you are going to learn the very basic structure of a C Program.

Basic Structure of C Program:-

        Preprocessor Statements

        Global Declarations

        int main()

        {

           Local Declarations

           Statement(1);

           Statement(2);

           ------------------

           ------------------

           Statement(n);

          return int_value;

       }

       User Defined Functions

 

  Example:-

    #include <stdio.h> //Preprocessor statement
#define PI 3.1416

const float radius=10.5; //Global declaration

int main()
{
float circumfernce; //Local declaration
circumfernce=findCicrum(); //User defined function call
printf("Circumference of the circle is %f",circumfernce); //printing the output
return 0;
}

float findCircum() //User defined function
{
return 2*PI*radius;
}

I will explain the parts, taking one by one.

1.Preprocessor Part:-

   Preproessor part as it's name suggests, is the set of statements which  will be executed at the beginning of the compilation. The preprocessor directives abilities to include header files, defining and expanding macros, conditional compilation and also for line control. At this level we are going to talk about only the preprocessor statements for including header files and for defining macros.

   1.I ncluding Header Files:-

        A header file is a file that allows programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers.

    General Syntax For Including header files in C :

           Type 1]      #include <filename>

                                   In this type the compiler will search for the file specified by the filename in the standard header file's directory.

                                   In our above program we used '#include<stdio.h>'  so that the compiler will search for a file stdio.h in the stnadrad  header files directory.

 

           Type 2] #include "filename"

                                   In this type the compiler will search for the file specified by the filename preferably in the project directory and also in all directories associated with the project.

 

 


        


No comments:

Post a Comment