Add

First Program And Variables in C++ Language

 


In this guide we will write and understand the first program in C++ programming. We are writing a simple C++ program that prints “Hello World!” message. Lets see the program first and then we will discuss each and every part of it in detail. 

Hello World Program in C++

/*

* Multiple line

* comment

*/

#include<iostream>

//Single line comment

using namespace std;

//This is where the execution of program begins

int main()

{

   // displays Hello World! on screen

   cout<<"Hello World!";

   return 0;

}

Output:

Hello World!

 

1. Comments – You can see two types of comments in the above program

// This is a single line comment

/* This is a multiple line comment

* suitable for long comments

*/

Comments as the names suggests are just a text written by programmer during code development. Comment doesn’t affect your program logic in any way, you can write whatever you want in comments but it should be related to the code and have some meaning so that when someone else look into your code, the person should understand what you did in the code by just reading your comment.

For example:

/* This function adds two integer numbers

 * and returns the result as an integer value

 */

int sum(int num1, int num2) {

   return num1+num2;

}

Now if someone reads my comment he or she can understand what I did there just by reading my comment. This improves readability of your code and when you are working on a project with your team mates, this becomes essential aspect.

2. #include<iostream> – This statements tells the compiler to include iostream file. This file contains pre defined input/output functions that we can use in our program.

3. using namespace std; – A namespace is like a region, where we have functions, variables etc and their scope is limited to that particular region. Here std is a namespace name, this tells the compiler to look into that particular region for all the variables, functions, etc. I will not discuss this in detail here as it may confuse you. I have covered this topic in a separate tutorial with examples. Just follow the tutorial in the given sequence and you would be fine.

4. int main() – As the name suggests this is the main function of our program and the execution of program begins with this function, the int here is the return type which indicates to the compiler that this function will return a integer value. That is the main reason we have a return 0 statement at the end of main function.

5. cout << “Hello World!”; – The cout object belongs to the iostream file and the purpose of this object is to display the content between double quotes as it is on the screen. This object can also display the value of variables on screen(don’t worry, we will see that in the coming tutorials).

6. return 0; – This statement returns value 0 from the main() function which indicates that the execution of main function is successful. The value 1 represents failed execution.


Variables in C++.


A variable is a name which is associated with a value that can be changed. For example when I write int num=20; here variable name is num which is associated with value 20, int is a data type that represents that this variable can hold integer values. We will cover the data types in the next tutorial. In this tutorial, we will discuss about variables.

Syntax of declaring a variable in C++

data_type variable1_name = value1, variable2_name = value2;

For example:

int num1=20, num2=100;

int num1,num2;

num1=20;

num2=100;

Types of variables

Variables can be categorised based on their data type. For example, in the above example we have seen integer types variables. Following are the types of variables available in C++.

int: These type of of variables holds integer value.

char: holds character value like ‘c’, ‘F’, ‘B’, ‘p’, ‘q’ etc.

bool: holds boolean value true or false.

double: double-precision floating point value.

float: Single-precision floating point value.

Types of variables based on their scope.

Before going further lets discuss what is scope first. When we discussed the Hello World Program, we have seen the curly braces in the program like this:

int main {

//Some code

}

Any variable declared inside these curly braces have scope limited within these curly braces, if you declare a variable in main() function and try to use that variable outside main() function then you will get compilation error.

Now that we have understood what is scope. Lets move on to the types of variables based on the scope.

1. Global variable

2. Local variable

Global Variable

A variable declared outside of any function (including main as well) is called global variable. Global variables have their scope throughout the program, they can be accessed anywhere in the program, in the main, in the user defined function, anywhere.

Global variable example

Here we have a global variable myVar, that is declared outside of main. We have accessed the variable twice in the main() function without any issues.

#include <iostream>

using namespace std;

// This is a global variable

char myVar = 'A';

int main()

{

   cout <<"Value of myVar: "<< myVar<<endl;

   myVar='Z';

   cout <<"Value of myVar: "<< myVar;

   return 0;

}

Output:

Value of myVar: A

Value of myVar: Z

Local variable

Local variables are declared inside the braces of any user defined function, main function, loops or any control statements(if, if-else etc) and have their scope limited inside those braces.

Local variable example

#include <iostream>

using namespace std;

char myFuncn() {

// This is a local variable

char myVar = 'A';

}

int main()

{

   cout <<"Value of myVar: "<< myVar<<endl;

   myVar='Z';

   cout <<"Value of myVar: "<< myVar;

   return 0;

}

Output:

Compile time error, because we are trying to access the variable myVar outside of its scope. The scope of myVar is limited to the body of function myFuncn(), inside those braces.

Can global and local variable have same name in C++?

Example having same name global and local variable.

#include <iostream>

using namespace std;

// This is a global variable

char myVar = 'A';

char myFuncn() {

   // This is a local variable

   char myVar = 'B';

   return myVar;

}

int main()

{

   cout <<"Funcn call: "<< myFuncn()<<endl;

   cout <<"Value of myVar: "<< myVar<<endl;

   myVar='Z';

   cout <<"Funcn call: "<< myFuncn()<<endl;

   cout <<"Value of myVar: "<< myVar<<endl;

   return 0;

}

Output:

Funcn call: B

Value of myVar: A

Funcn call: B

Value of myVar: Z

As you can see that when I changed the value of myVar in the main function, it only changed the value of global variable myVar because local variable myVar scope is limited to the function myFuncn().


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.