Thursday 26 December 2013

Magic Numbers

magic numbers


     Magic number are the unknown numbers used while writing a program , magic numbers should not be used in any c program as:-

1.  They degrade the quality of the program
2. They make it difficult to understand the program
3. They make program hard to modify in future
4. Even you cannot determine why you used the particular number ,if you go through your program after sometime.

 see the image below , this program contains magic numbers .................

now in the for loop it is difficult to determine why the loop ends at 10 ????


now see a better version of same program which uses pre processor directive( line 5 ). preprocessor does not end with ' ; ' (look again in line 5 ), preprocessor are used as a text editor, actually it is small program that runs before the compiler and replaces 'SIZE' with '10' throughout the program. so if you put ';' then it will replace 'SIZE ' with ' 10 ; ' and that will give you unexpected errors ( or even worst you would not get an error ).


there is still a magic number , yes 0, but most of the programmers don't take it as a magic number. Make sure your c program does not have magic numbers in future.
        

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 ).
   

Tuesday 24 December 2013

Introduction to the blog

Hello, friends I am writing this blog to help the new c programmers to learn coding, share their coding and to get the solution of the problem they face during the starting phase of the programming. I will also try to raise some important beginners topics . 


    As you may have noticed the aim of this blog is to learn c programming with the help of each other, so this will also increase your team work ability. In future I will give some questions and all the viewers can post their codes and I will try to find their errors and will try to give them suggestion to improve their coding.

  I also welcome you to post your coding related question and I will try to solve them as quickly as possible and other viewers can also help me in this process.

I will also try to add some projects in later stages so that we can solve them in modules and build it into a masterpiece.


*****YOUR SUPPORT IS REQUIRED TO MAKE THIS BLOG A SUCCESS *****