Friday 24 August 2012

Chapter 1 - The structure of a C program

Before we start  with programming let c understand what a c program looks like


1:  Preprocessor Declerations  
2:  Function Declerations  
3:  Global Variables  
4:  void main()  
5:  {  
6:     /*Local Vaciable Declarations*/  
7:     clrscr();  
8:     /*Your Program code*/  
9:     getch();  
10:  }  


I know you didn't understand a word written above, don't worry it will take time to get used to these programming constructs.

For now lets consider only the part of the code written in red.
To make things simpler to understand let me use a small code which prints a statement.

  /*  
    Title: simple code that prints a line.  
    Author: Ryan Sequeira  
    Created on: 24th August 2012  
  */  

  #include<stdio.h>  
  #include<conio.h>  

  void main()  
   {  
      // no need for local variables  
      clrscr();  
      printf("This is my first program");  
      getch();  
   }  
(use the options on the right top side of the code to view, copy or print the code)

Lets understand the code line wise

  • #include<stdio.h> and #include<conio.h>:  This is what a preprocessor directive looks like. The main use of this is to include ready made functions to our program. Notice how i used "printf("This is my first program");" in line 6. The implementation of printf is already written in the header file stdio.h which we simply include in our program. So #include<stdio.h> can be translate into simple english sentence as "Include (all the functions written in the header) stdio.h in my program".

  • void main() : This is where you start with your first line of code. The execution on the program starts from here. void main is the Main function or the Starting point of your code. It is also a function, but lets forget about that for now.
Note that <conio.h> i.e. the conio header is not a standard header and may not work with all the compilers. For those who are working on linux or unix (mac) platforms please skip conio.h along with the getch().

  • {....} : Let us consider void main as a electronic device, say a mobile phone. All the parts of the mobile phone are enclosed inside its casing. Similarly blocks of code are written within parenthesis {} also called IndentationsIndentations play a very important role in structuring the code and odd number of indentations are often the cause of many errors. So remember Every opening {  must have a closing }.

  • //no need for variables : Text written within /* .......  */ or // is ignored by the compiler. These lines are  called as comments and are written for description about the code. // is used for single line comment where as  /* ... */ is used for multiple line comments. You can consider /* as opening bracket and */ as closing bracket and everything written inside it will be the comment.

  • clrscr(): It is a function implemented in conio.h. The job of this function is to clear all the contents of the output screen before printing the new output.

  • printf("This is my first program"): printf() is a function that is used to print output in your output screen. It is implemented in the header file stdio.h  and anything written inside "..." will be printed.

  • getch(): I will explain you what this exactly is later. For now all you need to know is that this is implemented in conio.h header file. What it does is stops the execution of the program on line 13 until you press any key. Once you press any key the last line will get executed and the program will close.

If you have understood everything above try the activities listed below. If you have any doubts feel free to comment.

A complete tutorial to create and run the code can be found here.

NOTE: Remember every time the program pauses you have to press any key , this happens because u have interrupted the execution of the program by adding getch(). so if you know there are 2 getch's in the code you will have to press any key(preferable enter) 2 times for the program to end.

Activities:
  1. Try to close the computer screen and write down the structure of the C program on your own. Lets see how much of it you remember. Repeat this till u get it right.
  2. Copy the same code and see what happens if you remove  the block of code with      /* ..........*/ and the line  //no need for variables  .
  3. Try removing clrscr(); and check the output.
  4. Try the above code without getch();
  5. Add the following lines to your code after line 13 . repeat this any number of times and keep changing the text in " ... ".
             printf("This is a new line")
             getch();





No comments:

Post a Comment