Variables and Data-types in cpp

Unit 4: Variables and Datatypes


C++ variables and Data-types - int,float,double,char..


in the previous unit we've discussed C++ basics, in this unit we will go further and see what are the C++ variables and datatypes. 


print this page to save as pdf:) 

________________________________________________________________________

Variables


       "Variable is the name that is given to the memory location, where actually values get stored. In other words, variables are containers for storing data values."

________________________________________________________________________


Variable declaration syntax:


<data-type> <variable-name>=<value>;


Example:

// declaration of variable
int number = 10;


  • You can also declare a variable without assigning value, after declaration you assign value:

// declaration of variable// assign value after declaration
int number;number=10;


________________________________________________________________________


There are basic types of variables as follows:


Int

 An int variable is used to store an integer (positive, negative or zero).

Char

Char is used to storing character types (‘A’, ’4’, ‘#’).

Float

Float and double are also types for variables with large and floating point values (10.5).

Boolean

Boolean is used to store Boolean values (True or False).


________________________________________________________________________


Rules for declaration of variables 


Yes there are some rules to define the variable in our program otherwise you will end with some programming errors, Follow the following rules 


- A variable can have alphabets, digits, and underscore.


- A variable name has to start with alphabet and underscore only. It can't start with digit.


- No white space is allowed between variable names.


- A variable name must not be any reserved word or keyword e.g. char, float, etc

________________________________________________________________________

Scope of Variables:


        Variables have their area of functioning, and out of that boundary they don't hold their value, this boundary is called the scope of the variable.


There are three levels of scope:

1. Global Variable

2. Local Variable

3. Block Variable

________________________________________________________________________


1.Global Variable:


        A variable that has global scope can be seen in any other part of a C++ program, from a function definition to a block of code. A variable that is defined outside of the main function is a global variable.


        The primary problem with global variables is that, since they can be accessed from anywhere else in a program, they can be changed anywhere else in a program, which leads to hard-to-find logic errors

________________________________________________________________________


2. Local Variable:


        A local variable is a variable that is defined within a function definition. Variables that are defined within a function are visible only to code that exists within the function definition. The variable is said to be “local” to that function.


Here is an example of Global and Local Variables as follows:


#include <iostream>
using namespace std;
// Declaration of Golbal variable:
int num;
int main () {
   // Local variable declaration:
   int x, y;
   x = 10;
   y = 20;
   num= a + b;
   cout << num;
   return 0;
}


________________________________________________________________________


3. Block Variable:


    Variables defined in a code block can only be seen inside that block. A block is any code wrapped inside curly braces. The global space and function definitions have blocks, but for block-level scope, we are generally talking about an even tighter space of code, such as a for a loop.


#include <iostream>
using namespace std;
// Declaration of Golbal variable:
int num;
int main (){
   cout<<"Global num:"<<num<<endl;
   int num=2;
   cout<<"local num:"<<num<<endl;
   {    
        int num=3;
        cout<<"Block num:"<<num<<endl;    }    return 0; }


________________________________________________________________________


Data Types


        A data type is an attribute of data that tells the compiler or interpreter how the programmer intends to use the data. There are various variables used to store various information while writing a program in any language.

        Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.


________________________________________________________________________


Data types are divided into three parts:



1.Primitive Data Types:


        Primitive data types are predefined data and these data types are used directly by users to declare variables. The following image shows the primitive data types are available in C++:


2. Derived Data Types:


        Data types are derived from fundamental(built-in) data types. Variables of derived data type allow us to store multiple values of the same type in one variable but never allows us to store multiple values of different types.
  
 3. User-Defined Data Type:


             All these data types are defined by users themselves. Like, defining a class in C++ or a structure.

________________________________________________________________________



Previous: Installation of C++ Compiler 










 




إرسال تعليق (0)
أحدث أقدم