Thursday, February 13, 2014

5 tips for writing efficient C/C++ programs.

Programming the right way?

  • C and C++ are the most basic programming languages, and are often referred as god in the programming world.
  • Knowing to program the right way, and efficiently is very important.

Tips for efficient C/C++ Programs:


1)Always write your programs using proper spacing, and write in a structured manner.

Example:
#include<stdio.h>
void main()
{
clrscr();
printf("Hello World");
}

Instead of writing in the above way, write as follows:

#include<stdio.h>
void main()
{
              clrscr();
              printf("Hello World");
}

Make sure that you leave sufficient spacing before writing any codes. When writing inside any function, press the TAB key and leave some space, and write the entire code of that function, taking that amount of spacing before every line of code. When you write a function, inside a function, recursive functions, it becomes more important to leave spacing.

Example:

void main()
{
            clrscr();
            int i,j;
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    printf("Hello World");
                }
            }
}
Here, space is left before the second for loop, and inside the second for loop also some more space is left. Writing this way helps in understanding the code, and it also helps in correcting common errors like missing brackets. Make this a habit.

2)Make a rough note of your program. 

  • Write what you want in the output in a paper or notepad, and concentrate on it. 
  • Think logically, and make variables for each thing you require. For example if you are going to make a program for printing a line for 5 times, then first thing which should come to your mind is the loop. You will need a loop to display a line for 5 times. Start writing your code with the variables. 
  • Think what happens each time inside the loops you are going to create. Iterate the loops and see where you need to correct it. Scribble everything which comes to your mind on a paper, and then code it.

3)Think logically

  •  This is the part which is the most important part in creating programs. You may know all the syntax, but you just don't know what to use and how to create the program?
  • Think about the problem in parts. Divide the problem in a number of parts or "modules". 
  • See what you require in the problem. Do you need a lot of elements or things to store data? Do you need a lot of elements with the same data type? Do you need to do something MORE THAN ONE TIME? What are the things you need from the user? What will you print on the screen when the program runs? What will be your interface? What options will you give to the user for the problem? Think about all this very carefully.
  • Think iteratively. You need to think what will happen after every line is executed. 
  • Check for all the available functions and operators in the C/C++ language. You can do the same thing with multiple structures. In some cases, using arrays will be more simple then structures and classes.

4)Use proper syntax 

  • Although I don't need to tell this, but this is where, most people have problems with this.
  •  Always make a habit to use clrscr() and getch() in your programs. Many times your program runs correctly, but you simply cant see whats happening because you have not written getch().
  • Use smaller names for variables, it helps you to remember the variable names, and write faster. Instead of name, use n, instead of total_marks, use tot_m
  • Make functions for lines of code that are going to be needed frequently. For example you can make a function to input the data of a matrix.

5)Use Error Tracing 

  • If you are using the Turbo C/C++ compiler, you have an option of tracing your program. Tracing means to see how the program is running line by line.
  • If you have compiled your code without any errors, then you can use tracing. 
  • Use F7 key to start tracing your code. Use F7 for going to the next line. It will execute your code line by line, and you can see where you are getting problem. This helps most in finding out the logical errors, and solving infinite loops.

These where some tips to code efficiently. If you have any doubts, just leave a comment below.



4 comments:
Write comments
  1. I think instead of talking about Turbo C/C++ you should talk in perspective of new IDEs or at least which are not ancient like Turbo C/C++ like Dev C++. These tips for beginners, why to teach them old things when better new things are available. Moreover most of the people have Win7 nowadays, so Turbo C/C++ may not work properly for some people. There are few tricks but why to hassle when same thing can achieved simply and efficiently. I liked rest of the things and learned too.

    Thank you

    ReplyDelete
    Replies
    1. 5 Tips For Writing Efficient C/C++ Programs. - Virtual Lexicon Blog >>>>> Download Now

      >>>>> Download Full

      5 Tips For Writing Efficient C/C++ Programs. - Virtual Lexicon Blog >>>>> Download LINK

      >>>>> Download Now

      5 Tips For Writing Efficient C/C++ Programs. - Virtual Lexicon Blog >>>>> Download Full

      >>>>> Download LINK 2L

      Delete
  2. Thanks for the comments. C and C++ are still widely used as the base for teaching any programming languages. And most beginners have problems learning the concepts, thinking how to make the logic. That's the reason tips are generally given for C language. Because once you learn how to program in c language, you can then easily port to other languages. And yes, will consider again for other IDEs too!

    ReplyDelete