Subscribe by Email


Friday, September 9, 2011

Understanding the Structure of a C Program

The best way to get started with any programming language is to study a program, so in order to make you understand C program structure, we will take into consideration an example program whose code is:

1. /* this is a simple c program
2. Written to explain basic program structure */ -------------------------multiple line comment
3. #include //header file. ---------------------- single line comment
4. int main()
5. {
6. return 0;
7. }

This is the simplest C program and this program unfortunately doesn’t do anything. “#include” is called a preprocessor command. It tells the compiler to do processing before the compilation. This command tells the compiler to include the header file “stdio.h” in the program.

Functions are the basic building blocks of any program. The function “main()” is the most important function in any C program. You can say that this is the point of execution of a program. This function should exist as an entry point. Following the main is a pair of parentheses. These two parentheses indicate that the main is a function. Now coming to the opening and closing pair of braces in lines 5 and 7, are used to define the limits of a function or the program. Usually these braces contain executable statements of a function. The main() is preceded by word int. it means that this function returns an integer value. This value is returned using the “return” statement.

Now let’s take a program “abc.C” that does something. It’s same as the previous except that it has an executable statement between the braces in addition to the obligatory return statement. The executable statement calls a function which is already included in the header files as a part of the C library. The function is namely “printf()” and in order to make it print text the desired text is written with quotation marks within the parentheses of the function. The text will be displayed on the monitor when program is run. Notice the semicolon at the end of the executable statements. A semi colon in C is used as a statement terminator.

Consider the following statement:
printf("Hello World!\n");

Notice the “\n” character. The back slash is used to indicate to the compiler that a special character is being inserted. Here “n” indicates a new line. This is used whenever you want to print something in a new line. The function “printf()” prints the text and returns the carriage. Consider the following statements:

int index;

the key word int stands for integer and is used to declare a variable of type integer called index. Always keep in mind that the number of field descriptors and the number of variable definitions must be the same or else the runtime system will generate something we are not expecting. Another common and important part of C program structure is “comments”.Comments are optional but it’s good to include because they make the program more readable although they don’t mean anything to the compiler. Comments are of two types: single line comments and multiple line comments. Single line comments are used to give only one line description of a function or any other statement. A multiple line comment spans through several lines and is used to give a detailed description of a program or function.Look at the above program code for an example of comments. Another important part of a program are statements and expressions which combine variables and constants and can be assignments, function calls etc.Coming to the line breaks they are necessary to for good style formatting of your programs. This makes it easy for you to understand how your program flows when you want to maintain or modify it.You can also use “blank lines” after pre compiler declarations and declaration of new variables.Indentation should also be used in order to make your program more readable and to define the body of functions.


No comments:

Facebook activity