Wednesday 25 December 2013

Indentation in c

role of indentation is c 

    spacing and indentation role may be obvious for the college going programmers , but self learner struggle with this pretty simple thing. first of all i want to make clear that indentation is not at all require in c programming you even write the entire c code in a single line and it will still work . but , it will be quite horrible to write it this way ( in a moment i will discuss why ? ).


   So why we need indentation and spacing

      we do it for ourself , we the programmers need it ( not the computer/compiler). if you do correct indentation the readibility of the code increases and the chances of making the error reduces . The indentation is required so that next time you read your code it make sense. now, to demonstrate what i am trying to say show the images below and see the difference in two.

This image is showing the correct indentation and spacing...


This on looks horrible ....



    so you may have noticed in such small program it does not look that bad , but as the size of program increases it is life saving thing to do. you can use 3 or 4 level indentation ( some prefer three level indentation some prefer 4 level indentation ,basically it does not matter which one you choose as long as you are consistent , but do not use tab . 


Here is some challenge  for you :

1.  indent the following  program (even if you do not able to figure out what program actually do , do not worry about it ).


void printComputerDump (int memory[10][10], int accumilator , int instructionCounter , int instructionRegister, int toSeparate){

    int operationCode = instructionCounter / toSeparate ;
    int operand = instructionCounter % toSeparate ;

    printf("\n\nREGISTERS:\n"
          "accumilator                  +%4d\n"
           "instructionCounter            %4d\n"
           "instructionRegister          +%4d\n"
           "operationCode                 %4d\n"
           "operand                       %4d\n"
                         ,accumilator ,instructionCounter, instructionRegister, operationCode , operand);

     printf("\n\nMEMORY :\n\n\n");
     int i,j,k;

     for(k=0; k<MAX_MEMORY_ROW; k++){
     if(k==0)
      printf("      ");
      printf(" %4d  ", k);
     }
     printf("\n\n");

     for( i=0 ; i < MAX_MEMORY_ROW ; i++){

     printf("%2d    ", i*MAX_MEMORY_COLUMN);

     for(j=0 ; j < MAX_MEMORY_COLUMN ; j++){

   if(memory[i][j] == SENTINEL){
     printf("  XXX  ");
    }
   else{
   printf("+%4d  ", memory[i][j]);
  }
 }
printf("\n");
 }
}


---> post your answers in the comments below.  ( I know this code has certain bad programming things like magic numbers, and the code is well written ,So it is quite difficult to indent it , but trying to indent this will teach you the importance of spacing and indentation ).
   

2 comments:

  1. need any help regarding spacing and indentation , feel free to ask..:)

    ReplyDelete
  2. thank you..for this useful information.

    ReplyDelete