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"

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:

Tip 3:


#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:

More will be updated soon....