Programming in C: Unit I (b): Introduction to C

Structure of a C Program

A C program is composed of preprocessor commands, a global declaration section, and one or more functions

STRUCTURE OF A C PROGRAM

A C program is composed of preprocessor commands, a global declaration section, and one or more functions (Figure 2.2).

The preprocessor directives contain special instructions that indicate how to prepare the program for compilation. One of the most important and commonly used preprocessor commands is include which tells the compiler that to execute the program, some information is needed from the specified header file.

In this section we will omit the global declaration part and will revisit it in the chapter on Functions.

A C program contains one or more functions, where a function is defined as a group of C statements that are exe- cuted together. The statements in a C function are written in a logical sequence to perform a specific task. The main () function is the most important function and is a part of every C program. The execution of a C program begins at this function.

All functions (including main()) are divided into two parts the declaration section and the statement section. The declaration section precedes the statement section and is used to describe the data that will be used in the function. Note that data declared within a function are known as local declaration as that data will be visible only within that function. Stated in other terms, the life-time of the data will be only till the function ends. The statement section in a function contains the code that manipulates the data to perform a specified task.

From the structure given above we can conclude that a C program can have any number of functions depending on the tasks that have to be performed, and each function can have any number of statements arranged according to specific meaningful sequence.

Note

Programmers can choose any name for functions. It is not mandatory to write Function1, Function2, etc., but with an exception that every program must contain one function that has its name as main().

Programming in C: Unit I (b): Introduction to C : Tag: : - Structure of a C Program