Pages

Wednesday, 22 June 2011

Structure of a C Program



Every C program consists of one or more distinct units called functions. Each function has unique name. One and only one of the constituent functions of a C-program must be named main(). It is the main function, which is executed when the program is run. A function may call another function, which executes and return computed value to the calling function.

Each function has a name, an optional list of input parameters (also called arguments)with their individual data-type, and a return data-type T.

A compound statement is a group of zero or more statements enclosed within curly braces ( ). Compound statements may be nested i.e., one compound statement may exist inside another one.

A C-statement is a valid C-expression delimited by semi-colon.

The following rules are applicable to all C-statements:

• Blank spaces may be inserted between two words to improve the readability of
the statement. However, no blank space is allowed within a word.

• Most of the C-compilers are case-sensitive, and hence statements are entered in
small case letters.

• C has no specific rules about the position at which different parts of a
statements be written. Not only can a C statement be written anywhere in a
line, it can also be split over multiple lines. That is why it is called
free-format language.

• A C-statement ends with a semi-colon (;).

Functions

Every C program is structured as an assembly of one or more distinct units called functions. Each function comprises of a set of valid C statements and is designed to perform a specific task. Each function is given a unique name for reference purposes and is treated as a single unit by the C-compiler. A function name is always followed by a pair of parenthesis, i.e., ( ).

The statements within a function are always enclosed within a pair of braces { }. Every C program must necessarily contain a special function named main ( ). The program execution always starts with this function. The main function is normally, but not necessarily, located at the beginning of the program. The group of statements within main ( ) are executed sequentially. When the closing brace of the main function is reached, program execution stops, and the control is handed back to the operating system.
Whenever a function is called, it returns to the caller the value it is supposed to return.
Schematically, a C-function may be depicted as:


The C-program code for this function looks like:
Int FindMax(int a, int b)
{
Statement 1;
Statement 2;
return (p) ; //p is the integer value returned by
//this function
}
In plain English it reads: function, whose name if FindMax, takes two integer type arguments, process it some way (i.e. executes the statements contained within) and returns an integer value to the caller, which can be used by other statements of the program.

c programmming online,basic of c language,structure of c programming,c language statements,Compiling a C Program,Pseudo Codes in c,basis data type in c ,variables in C Program,constants in C Program,c operators in C Program,special operators in C ,Decision Making,Branching & Looping in C Program,if statement in C Program,for loop in C Program

No comments:

Post a Comment