Saturday, June 29, 2013

Understanding a Program in C

Let us know more about a C program. A C program contains many components. If you observe the previous example, you can identify many things. Let us know more about what they are, why we have used them and how to use them.

Consider another example of C program which contains more components, than in the previous example and know what they are.


  1. /*Second Example Program */
  2. #include<stdio.h>

  3. main()
  4. {
  5. /* Declaration of variables */
  6. int a,b,c;
  7. /* Prompting the user to enter value of a */
  8. printf("Enter a value for a : ");
  9. /* Taking the value of 'a' */
  10. scanf("%d", &a);
  11. /* Prompting the user to enter value of b */
  12. printf("\nEnter a value for b : ");
  13. /* Taking the value of 'b' */
  14. scanf("%d", &b);
  15. c=multiply(a,b);
  16. printf("\nThe product of %d and %d is %d",a,b,c);
  17. return 0;
  18. }

  19. int multiply(int a, int b)
  20. {
  21. /*Multiplying the values received */
  22. int x=a*b;
  23. /* Returning the result to main function */
  24. return x;
  25. }

Now, if you observe the above program, we have many new things. Let us discuss about each part now.

Comments:

The lines 1,6,8,10,12,14,23,25 are the comment lines. Comment lines are just for understanding of the program by a new user. They doesn't take any part in the development of the program but they are helpful in understanding the developer's thought. They are optional in using. But it is a good practice to use the comments. 

A comment line is written between /* ..... */

We can use the comments anywhere in the program. But nested comments are not allowed i.e., a comment inside a comment line is not allowed.

#include:

The line 2 represents the #include directive. It is used to import some contents into our program during compilation. In the above program, we have used STDIO.H which is a header file with some predefined functions embedded in it. This is provided with the C compiler. Generally these files are called as header files with an extension of ".h". By writing such a line, we are importing the components of that header file into our program. There are many header files, which we use based on our need. Also we can write our own header file and can import them to the program.

main():

main() function is the heart of the C program. Every C program must have a main() function. Every program execution starts with a main function. For a C program, there should be one and only one main function and this function in necessary for a C program to exist. This functional block starts with a { and ends with a }. All the code we write between the '{' and '}' comes under main section. This is called block of statements.

Note: We have said that we can't write a program without main, but we can do it. If you want to know it, just follow this link.

Variable Declarations:

Consider the line 7, it is nothing but the variable declaration. Variables are declared by specifying the type of the variable and the variable which is going to hold. We will know about the variables later.

printf():

printf() is a predefined function in C. This function is used to display some text on to the user screen. The message which we write between the double quotes in the printf() statement is displayed on the screen. To know more about the printf(), click here.

scanf():

scanf() is a function used to take something from the user into a variable. We specify the type of the value we are taking in, by representing it in the form of '%specifier". For example %d is for integer, %c is for character etc. and then we specify the variable which should hold the value, by representing it as '&variable'. To know more about the scanf() function click here.

function:

Functions are generally two types.
  • User defined functions 
  • Predefined functions
printf(), scanf() etc., comes under predefined functions. The functions which we write comes under the user defined functions. 

In the above example, the lines 21-27 represents the user defined function. In this function, we have taken two integer values and performed the multiplication on them and returned the product to the main function again. Consider the line 16, In this line, we have specified the function multiply() to take values of a and b and the result should be stored in c. We will take more about the functions in the later topics. But as of now, just remember that we can write our own functions in C.

return:

return statement is generally written at the end of the function. The statements after the return statement are not executed. We generally write return if we are supposed to return anything i.e., if a function is supposed to give some value to the call, then we use written. In the above program, we have used return as 'c' is expecting the function to return some value for it.

The default return type of main function is int. Hence we written some integer value at the end of the main() function. To know more about return statements, click here.