Tips For C++ Programmers - Read & Learn

Discussion in 'Computer Science | IT | Networking' started by Vishal0203, Aug 28, 2012.

  1. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    Small tips for programmers::

    NOTE-> THESE TIPS ARE SUPPORTED IN DEV-C++ WHICH USES A GCC COMPILER. I SUGGEST EVERYONE TO USE DEV or VISUAL C++ BECAUSE IT WILL SURELY ENHANCE YOUR PROGRAMMING SKILLS AND INTELLIGENCE.

    1) Most of us use clrscr( ); to clean up the output screen. Well, this function is not a standard function to be used in your program. Use system("cls"); which is included in stdlib.h this function is more preferable than clrscr();



    2) While programming for C++, instead of
    Code:
    #include <iostream.h>


    use

    Code:
    #include <iostream>
     
    using namespace std;
    as the programming environments have been developed and none of the people use the ancestor blue screen programming environment. Similarly, the header files ctype.h, assert.h etc can be directly writen as cctype, cassert (without .h extension)

    3) In C++, using getch( ) to hold the output on screen is a bad practice. Which is not a standard function provided by ANSI C. Use
    Code:
    fflush (stdin);  // cstdio header
     
    std::cin.get();  // iostream header

    4) DO NOT USE void main( ) since it returns a garbage value to the main function, which makes it impossible to be called explicitly in some other program.
    use int main( ) so that you know what value it returns.

    5) And, at the end I'd like to tell something about presentation. Presentation of code is very important in all aspects. The code must be understandable not only by you but also to the other person. It doesn't matter to whom you are presenting the code. I've seen people (mostly my classmates) writing the code in very clumsy way, and when they ask me any doubt, I'm unable to understand their code, just because their way of presentation is too bad.
    The same thing must be kept in mind while asking the doubts on the forums. Your codes must be understandable by the person who is seeing it, or else they loose interest in helping you out..

    > Writing comments is a very good habit. Write a comment about the action of the statement in your code. It must be done for all the loops you're using. If you're able to determine where you are getting an error, write a comment indicating the error.

    > Make your code more expanded and clear. Use the white spaces (space bar or tab keys ). They don't effect your program in anyway as the compiler skips all the white spaces used. But, it helps the user to understand the code easily.
    ex:

    Code:
    int main() {printf("HELLO CEANS"); return 0;}
    the compiler faces no problem in faces no problem in executing the above code. But is it presented well?? Well, everyone knows NO! We can instead write it in different way as

    Code:
    int main()
    {
      printf ("HELLO CEANS");
      return 0;
    }

    The same things can be done while using the loop conditions or conditional statements

    Code:
    // make your code look clear...
     
    for(i = 0 ; i < condition ; i++)  // spaces between the variables and operators
    {                     // opening and closing braces are in same line
      .............        // loop statements are well organized
      .............
    }



    So that's it for now! Have a good day! CHEERS :cean:
     
    • Like Like x 12
    • Agree Agree x 1
    #1
  2. Ankita Katdare

    Moderator

    Engineering Discipline:
    Computer Science
    Vishal0203 That is a really useful post. I have pinned it. Keep writing useful articles and posts as tutorials for CEans. :cean:
     
    • Like Like x 1
    #2
  3. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    AbraKaDabra
    Thanks a lot! :) I'll update this post if I get along some new things to make the program efficient!
     
    #3
  4. PriyaJ

    PriyaJ Apprentice

    Engineering Discipline:
    Electronics & Communications
    Thank You. That was really useful.:)
     
    #4
  5. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    PriyaJ
    good to know that! :)
     
    #5
  6. monujatt

    monujatt Apprentice

    Engineering Discipline:
    Computer Science
    With reference to this -> Small tips for programmers::
    Your code should have proper comments ...sometimes programmer itself not able to understand what an API does(even that is written by him).... :)
     
    #6
  7. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    monujatt
    Yeah! I've seen such people.. o_O
     
    #7
  8. sulochana anand

    sulochana anand Apprentice

    Engineering Discipline:
    IT
    good knowledge given by u.thanx
     
    #8
  9. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
  10. gaurav91

    gaurav91 Certified CEan

    Engineering Discipline:
    IT
    i too had worked with c++!! but i didn't knew most of it ..true knowledge ..
     
    #10
  11. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    gaurav91
    There are many more things.
    Like the use of iomanip.h
    In C++, it is another header which contains the functions, which can make your presentation on output screen better!! :)
     
    • Like Like x 1
    #11
  12. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    ONE MORE THING I FORGOT TO ADD IN THE POST!

    Avoid using conio.h header

    conio.h is not considered as the standard library. All the compilers may not support this header. Hence we avoid using it.

    So, the functions like clrscr(); and getch(); are also considered as the non standard functions. As, I've earlier mentioned, the alternative for clrscr(); is system("cls");
    Similarly, for getch(); the alternative is getchar(); which is in stdio.h

    Do not forget to flush the input stream before using getchar(); and its equivalent in C++ cin.get();
     
    #12
  13. gaurav91

    gaurav91 Certified CEan

    Engineering Discipline:
    IT
    hey since when r u working in c++?
     
    #13
  14. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    I'm a second year student, so, Just 3 months.. :)
     
    #14
  15. gaurav91

    gaurav91 Certified CEan

    Engineering Discipline:
    IT
    wow awesome buddy! so where r u studying c++ from i mean which books do u refer?
     
    #15
  16. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    The books doesn't have anything related to all this. The books just teach us the basics. To make your skills advanced, do a research. :)
    The best thing, search the internet, join useful forums! There are many intellects across the world who are keen to help you out! And they even help making your programs more efficient!
    These tips are used for efficient programming!
     
    #16
  17. gaurav91

    gaurav91 Certified CEan

    Engineering Discipline:
    IT
    that's y asked becoz ..i didn't find all these tips or anything related anywhere in the books .. but all these tips r one of the unknown.. and u r sharing ur knowledge that's good.. for people like me also :)
     
    #17
  18. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    :) I'm glad it's helpful.
    majority of people in India are unaware of these things as we are never taught about all this in our college. So, the forums like CE create good platform of learning!
     
    #18
  19. David Emperor

    David Emperor Certified CEan

    Engineering Discipline:
    IT
    Hey dude :) add for people
    that scanf() and printf () are working fast than cin and cout
    the view of scanf is scanf ("%/*const char * format*/", variable);
    formats are d- decimal integer , e,E,f,g,G - float types, u - unsigned int, h, H- hexidemical number, s - string, c-char , o - octal integer
    for example scanf ("%f", &n); printf ("%f", n);
    for newline type printf ("\n"); for tab printf ("\tab");
    ;)
    when you use functions the value that you gave to function is being copied in parameters, so if you want make changes in the given variables you must use
    Code:
    void f(int& k, int& s)
    {...}
    int main()
    {...
        f(n,m);
    ...}
    instead of
    Code:
    void f(int k, int s)
    {...}
    int main()
    {...
        f(n,m);
    ...}
    for calculating 2^k fast use
    Code:
    int s=1;
    s=s<<k;
    for swaping values of variables use
    Code:
    a=a^b;
    b=a^b;
    a=b^a;
    I think that's enough :)
    sorry for weak english :sick:
     
    • Like Like x 1
    #19
  20. Vishal0203

    Vishal0203 Maniac

    Engineering Discipline:
    Computer Science
    you are right! :)
    But, these are the tips for using c++. scanf and printf functions belong to C
    Hence, you cannot use them when you are asked to program in c++.

    Remaining things are all fine.. :)
     
    #20

Share This Page