Showing posts with label Chapter3-Introduction to C Language. Show all posts
Showing posts with label Chapter3-Introduction to C Language. Show all posts

Tuesday, October 12, 2010

Structure of a C Program with an example


STRUCTURE OF A C PROGRAM:
The basic structure of a C program is as shown below –
Documentation Section
----------------------------------------------
Link Section                                   
----------------------------------------------
Definition Section
----------------------------------------------
Global Declaration Section
main() Function Section
{
           Declaration Part
           ------------------------
           Executable Part
}
Subprogram section
            Function 1
            Function 2
                    .
                    .
                    .
            Function n

Description:
·         The documentation section consists of a set of comment lines giving the name of the program, the author’s name and other details, which the programmer would like to use later.
·         The link section provides instructions to the compiler to link functions from the system library.
·         The definition section allows symbolic constants.
·         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.
·         Every C program must have one main() function section.
o   This section contains two parts declaration part and executable part.
o   The declaration part declares all the variables used in the executable part.
o   There should be at least one statement in the executable part.
o   These two parts must appear between the opening and  the closing braces.
o   The program execution begins at the opening brace and ends at the closing brace.
o   The closing brace of the main function section is logical end of the program.
·         All statements in the declaration and executable parts end with a semicolon.
·         The subprogram section contains all  the user-defined functions that are called in the main function. The main function is very important compared to other sections.

Example:
main()
{
            /* Printing Begins */
            printf(“Hello World!\n”);
            /* Printing Ends */
}
This program displays Hello World! on the screen.
Description-
·         The first line is where the execution begins. There must be exactly one main() in every program.
o   The empty brackets () indicate that the function does not have any arguments.
o   ‘main’ can be written in any of the following ways –
main()                        int main()                               void main()  
main(void)                int main(void)                      void main(void)
o   Here int means main returns an integer and if void returns nothing.

·         The function is followed by { indicating the beginning of function body.
·         The first line inside ‘main’ function is a comment line (which begins with /* and ends with */). Such comment lines are used to increase the readability of a program.
o   Comments are non-executable.
o   They cannot be nested                   /*...... /* ....... */ ....... */ is not valid.
·         The second line has a function printf, which is a predefined C function for printing output.
o   This function causes everything between the starting and ending quotation marks to be printed out.
·         The \n is a newline character, which instructs the computer to go to next (new) line. It is similar to the return key of the keyboard.
o   No space is allowed between \ and n.
·         ‘printf’ function is defined under the pre-processor directive stdio.h
o   ‘stdio.h’ is a header file used for calling functions for input and output.



READING DATA FROM KEYBOARD:
Data can be inputted and stored into a variable using a predefined function called scanf.
·         ‘scanf’ is also a part of the directive ‘stdio.h’.
·         Its syntax is –
scanf(“control string”, &variable1, &variable2, ...);
Example: -                 scanf(“%d”, &n);
While executing a program, this statement makes the cursor to blink on the screen until an integer is inputted from the keyboard and return key is pressed.

After an integer is keyed in, that entered value will be saved in the variable n, so that keyed in value can be accessed later using the variable ‘n’.
·         Thus scanf makes the program interactive and makes it user friendly.

History of C

INTRODUCTION TO C:
·         C is a programming language developed at AT&T’s Bell laboratories of USA in 1972.
·         It was designed and written by a system programmer Dennis Ritchie.

HISTORY OF C:
The following flow diagram shows the evolution of C –



·         The root of all modern languages is ALGOL, introduced in 1960s. It was the first computer language to use block structure.
·         In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming Language) mainly for writing system software.
·         In the year 1970, using the concepts of BCPL, Ken Thompson developed a language called B. It was used to create earlier versions of UNIX operating System at Bell Laboratories.
o   Both B and BCPL were typeless system programming languages
·         C was evolved from ALGOL, BCPL and B by Dennis Ritchie at the Bell Laboratories in 1972.
o   C added the concept of data types
o   Since C was developed along with the UNIX operating system, it is strongly associated with UNIX.
·         The language became more popular after the publication of the book “The C Programming Language” by Brian Kernighan and Dennis Ritchie in 1978. The book was so popular that the language came to be known as”K & R C”.
·         Later on it was made an American Standard (ANSI C) and then International (ISO).
·         C99 is a standardized version of C which was done in 1999; so the name C99.

C language became popular because of the following reasons –
§  C is a robust language with a rich set of built-in functions and operators.
§  The C compiler combines the capabilities of an assembly language with the features of a High Level Language and therefore it is well suited for writing both system software and business packages.
§  C language is highly Portable – This means that C programs written for one computer can be run on another with little or no modification.
§  C language is well suited for Structured programming, thus user needs to think a program in terms of function modules or blocks. – This modular structure makes program debugging, testing and maintenance easier.
§  A C program is basically a collection of functions that are supported by the C library. We can continuously add our own functions to C library.