Saturday, November 8, 2014

C++ Beginners - Few Tips

Hi, Everyone

Hope you all are good.
From last year I was trying to return back and finally I'm back. :)
Lot's of people have requested me to return back and appreciated my blog.
I'm glad that so many people liked my blog so much. Thanks to all who spend there precisions time to read it.
Thanks Again!! :)

Today, in this post I want to point out few basic things which we need to know as a Professional Developer. When we are in college and Programming Beginner we never know or didn't get the opportunity to learn or we learn it but it's not a good practice to apply at professional world. This post will help you find your basic questions and tips to write a professional code.

Before we start let me share two important Mantras: 
"Never trust end users"
"You are responsible for your code"

Tips/Suggestions:
Tip 1.
#include <iostream>                                    // 1  
using namespace std;                                    // 2  
                                                                             

int main(int argc, const char * argv[]) {      // 3 
     cout << "I'm a Programmer";                // 4 
    return 0;                                                    // 5 
}                                                                           


Let me explain you the code.

Line 1: #include <iostream> 
Line beginning with a hash sign (#) are preprocessor directives which instructs the preprocessor to include a standard C++ code, known as header. Here, iostream has been included to allows our code to perform standard input and output operations.

Thinking, What's preprocessor? Just go though this image.



Line 2: using namespace std;
A namespace defines a new scope. They provide a way to avoid name collisions (of variables, types, classes or functions) without some of the restrictions imposed by the use of classes, and without the inconvenience of handling nested classes. 

std namespace is declared in iostream.


I shall always recommend to write Line 4 as std:cout << "I'm a Programmer"; in place of declaring namespace at top.

Why? Suppose there are two variables with same variable name in two separate namespace. Now, if you include both of those using "using". At the time of implementation section it's confusing which variable will be included which may lead to un expected results.



"Namespaces are a powerful addition to an already powerful language, giving the programmer more flexibility, provided he knows how to take advantage of it."


Line 3: int main(int argc, const char * argv[])
When our program will start executing OS will call main method to start it's operation. 
We can recevice paramter to our code. Say our program is complilted and output file name is DemoCode.

Now if we run our code like this:
$ DemoCode India USA UK
argc will give 4 as number of arguments and argv array will have 4 arguments, DemoCode, India, USA and UK.

Line 4: cout << "I'm a Programmer";
It's a way to print a message in Console/output stream.

Line 5: return 0; or return EXIT_SUCCESS;
Once a program has a successful execution it return 0 or use macro EXIT_SUCESS to OS. Otherwise, we can return 1 or EXIT_FAILURE macro which denotes execution failed.
Value of EXIT_SUCCESS and EXIT_FAILURE are declared in header <cstdlib>
                                                                                                                                                                    

Tip 2: 
Comma operator ( , )

For example:
int b;
int a = (b=5,b+2);

In this code, second line first the value 5 will be assigned to b after that 2 will be added to b and last it will be assigned to a. So, a will be 7.

Read More: http://en.wikipedia.org/wiki/Comma_operator
                                                                                                                                                                  

Tip 3:

Macros

Format:
#define [identifier name] [value]
Macro is familiar word for Beginners and a common use most of you may have seen:

#define M_PI 3.14159                                // Approx. value of PI used


float radious = 25.0; 

float circumference = 2 * M_PI * radious;
Read More: https://gcc.gnu.org/onlinedocs/cpp/Macros.html
                                                                                                                                                                

More will be updated soon....

13 comments:

  1. I am a newbie in the world of programming with c++ and I found your tips really helpful. C++ tutorial in urdu

    ReplyDelete
  2. I like the diagrams used to explain the programming. That's what I'm trying to implement in my C++ Website to cater for beginners. Check out my website
    http://cppbetterexplained.com

    ReplyDelete


  3. Thanks for sharing the very useful info about clanguage and please keep updating........

    ReplyDelete
  4. Nice blog!! If you need any technical issues just call our Bullguard Support Number UK @0808-238-7544 Bullguard Phone Number UK

    ReplyDelete
  5. Thank for sharing this information about Bullguard. There are so many things that bother Bullguard users and this is one of them. However, users can find solution for other issues on Bullguard Phone Number UK, where I am working 24/7 with my team of technicians.

    ReplyDelete
  6. Web services are client and server applications that communicate over the World Wide Web’s (WWW) HyperText Transfer Protocol (HTTP). Web services provide a standard means of inter operating between software applications running on a variety of platforms and frameworks. Web Design Services

    ReplyDelete
  7. After I applied the help given in this blog in order to get my security software fixed, it is now back to normal working, I am really very thankful to the writer.
    Bullguard Support Number UK

    ReplyDelete
  8. I am glad to see such nice informative and helpful content keep writing and sharing more often.
    http://www.gmailhelplinenumber.co.uk/

    ReplyDelete