Basic Structure Of C Program


Here you learn the basic structure of C program.


 Preprocessor Directives
 Global Declaration
 main()
 {
    Local Declaration
    C Statements
 }
 func()
 {
 }


Now we discuss about the above structore in details.

Preprocessor

A preprocessor is a system program that modifies a C program prior to its compilation.

Preprocessor Directives

A C program line beginning with a # (pound sign) provides an instruction to the compiler. Preprocessor directives are commands that give instructions to the C compiler, whose job is to modify the text of a C program before compiled. A preprocessor directive begins with a number symbol (#) as its first non-blank character. The two most common directives are #include and #define.

Library

“A collection of useful functions and symbols that may be accessed by a program”, the library files contain built-in functions in translated form (Object Code). The library files having extension “.lib”, For example: cs.lib The C language explicitly defines only a small number of operations: Many actions that are necessary in a computer program are not defined directly by C. Instead, every C implementation contains collections of useful functions and symbols called libraries. The ANSI (American National Standards Institute) standard for C requires that certain standard libraries be provided in every ANSI C implementation. A C system may expand the number of operations available by supplying additional libraries; an individual programmer can also create libraries of functions. Each library has a standard header file whose file extension with the symbols .h.

The “#include” directive gives a program access to a library. This directive causes the compiler to insert definitions from a standard header file into a program before compilation.

Header Files

Header files are part of the C compiler and contain definitions of standard library functions. There are several header files. Each header file contains definitions of one type of functions only. For example the “math.h” header file contains definitions of mathematical functions available in C language. Each header file has an extension “.h”. The preprocessor directive “include” is used to add a header file into the program. The name of the file is written in angle brackets (<>) after “#include” directive. The syntax to include a header file is:
         #include
         #include <stdio.h>
Giving the name of the header file in angle brackets specifies that the header fie is located in the default include directory of the compiler program. The name of the header file can also be written in double quotes. If your header file is not contain in default include directory of compiler then the address of the file with file name and extension is used in double quotes “ ” in the include directive. Header file with in an angle brackets used in include directive, when your header file is in default include directive.
        #include “name of the header file”
        #include “e:\myheader\my.h”

main() function

main() function indicates the beginning of a C program. The “main()” must be included in every C program. When a C program is executed, the control goes directly to the main() function. The statements within this function are the main body of the C program. If main() function is not included, the program is not compiled and an error message is generated. The compiler always call main() function and execute only main() function. All other functions in the program is not executed until main() directly or in directly call them.
The syntax of the main() function is:
main()
{
   Program statements…
}

C Statements

The statements of the program are written under the main() function between curly braces { }. These statements are the body of the program. Each statement in C ends with a semicolon (;).

Functions

We "ll" discuss this topic in details in functions chapter.

ADVERTISEMENT