STRUCTURE OF A C PROGRAM

A program in C consists of one or more functions, one of them must be called the main, the controlling function. The program execution always begins by executing the main function. Additional function definitions may precede or follow main.

A function description consists of the following components :

1. A function heading, which consists of the function name, followed by an optional list of arguments enclosed in parentheses.

2. A list of argument declaration, if arguments are included in the heading.

3. A compound statement, which comprises the remainder of the function.

The arguments are symbols that represent information being passed between the function and other parts of the program. (Arguments are also referred to as parameters.)

Each compound statement is enclosed within a pair of braces, i.e., { }. The braces may contain combinations of elementary statements (called expression statements) and other compound statements. Thus, compound statements may be nested, one within another. Each expression statement must end with a semicolon (;). Comments remarks) may appear anywhere within a program, as long as they are placed within the delimiters /* and */ (e.g., /* this is a comment */). Such comments are useful for identifying the principal features of the program and also for explaining the underlying logic.

Consider the flow-chart in fig 1.1 depicting how the roots of a quadratic polynomial can be found.


Example 1.1 :

Based on this flowchart, a sample C program for finding the real roots of a quadratic polynomial is given below:

# include /* a preprocessor directive to include the file stdio.h */

# include

/*program to find the real roots of a quadratic polynomial */

main()

{

/* type declaration of the variable*/

float a, b, c, discriminant, d, w, root1, root2;

/*function declaration*/

float compute_discriminant(float x,float y,float z);

printf(“Give the values of the coefficients a,b,c of the polynomial :”);

scanf(“%f %f %f”,&a,&b,&c);

fflush(stdin); /* flushing the buffer associated with standard input*/
discriminant=compute_discriminant(a,b,c);/*invoking a function */

d= 2 * a;

if(discriminant == 0)

printf(“The roots are equal - %f\n”,- b / d);

else

if(discriminant <>

printf(“The roots are complex\n”);

else

{

w=sqrt(discriminant);

printf(“The roots are real and different\n”);

root1=(-b - w) / d;

root2=(-b + w) / d;

printf(“Root1 = %f, Root2 = %f\n”,root1,root2);

}

}

/* Function definition */

float compute_discriminant(floatx, float y, float z)

{

float d; /*local variable declaration*/

d=y * y - 4 * x * z;

return d;

}

Here the main function utilises a separate programmer defined function called compute_discriminant, to find the value of the discriminant. Within the programmer defined function, x, y, z are arguments (also called as parameter). The values for these parameters are supplied from the main. The values of a, b, c are supplied to x, y, z respectively. Then the programmer defined function performs the calculations and the result is returned to the main.

This function is called in the main in the statement :

discriminant=compute_discriminant(a,b,c);

The above statement invokes the function compute_discriminant with arguments a, b and c. The function main transfers the control of the program execution to another function (compute_discriminant). The called function finally returns the control back to the calling function (in this case main) with the result of the calculation performed by it.

The main function also includes a function declaration, which indicates that compute_discriminant accepts floating-point arguments and returns a floating-point value.

Introduction To ‘C’

C is a general-purpose, structured programming language. Structured Languages have a characteristic program structure and associated set of static scope rules (static scope rules are discussed in detail later). The structured programming concepts originated in the language ALGOL 60, one of the most important early languages. They have been adopted in other languages because of their elegance and effect on implementation efficiency. In a structured or to be more specific Block-structured language, each program or subprogram is organised as a set of nested blocks. The chief characteristic of a block is that it introduces a new local referencing environment. A block begins with a set of declarations for names (variable declarations, type definitions, constant definitions, etc.), followed by a set of statements in which those names may be referenced. In C, there is a block structure, but it exists only within a single subprogram.

C was originally developed in the 1970’s by Dennis Ritchie at Bell Telephone Laboratories, Inc. (now AT&T Bell Laboratories). In 1978, Brian Kernighan and Ritchie published a comprehensive description of the language C. The Kernighan and Ritchie description is commonly referred to as “K&R C”. Following the publication of the K & R description, computer professionals, impressed with C’s many desirable features, began to promote the use of the language. Since 1980’s, the popularity of C has become widespread. Most commercial implementations of C differ somewhat from Kernighan and Ritchie’s original definition. This has created some incompatibilities among different implementations of the language, thereby diminishing the portability of the application program developed using C. Consequently, the American National Standards Institute (ANSI) proposed a standardised definition of the C language (ANSI committee X3J11). Most commercial C compilers and interpreters are expected to adopt the ANSI standard.

The statements in C resemble other high-level programming languages such as Pascal and FORTRAN-77. C also supports certain additional features, that allow it to be used at a lower level, thus bridging the gap between machine language and the more conventional high-level languages. This flexibility allows C to be used for systems programming (e.g., for writing operating systems) as well as for applications programming (e.g., for scientific computation, mathematical equations, or for business data processing).

It is possible to develop concise source programs in C mainly due to the large number of operators available in the language. Although the language has a relatively small instruction set, it is possible to include extensive library functions that enhance the basic instructions. Furthermore, the language encourages users to write additional library functions of their own. Thus, the features and capabilities of the language can easily be extended by the user.

C compilers are available for all types of computers ranging from PCs, workstations to super computers. The compilers are usually highly optimised, and generate object programs that are small and efficient. In addition, C programs are highly portable, as compared to the programs written in other high-level languages. The reason for this is that machine dependent features are often handled by library functions. Thus, every version of C is accompanied by its own set of library functions, which are written for the particular characteristics of the host computer. These library functions are relatively standardised with a common access protocol for these functions. Therefore, most C programs can be processed on different machines with little or no alteration.