2. Basics Of DataType and Scopes In CPP

Previous ( 1.cpp ) << 

Next (3.cpp ) >>

 1. Variable and DtatTypes 

    int marks ;   // Variable Declare
    marks = 10; // assignment
    int marks = 10 ; // Initilization or ( declaration and assignment)



    #include<iomanip> // for setprecision(int)
   
int main (){
        // Variable Init (Decleration + Assignment)
        int marks = 10;
        float temp = 28.5;
        double pi = 3.14156738;
        
        cout << marks <<endl;
        cout << temp << endl;       // output : 28.5
        cout << pi << endl;           // output : 3.14157
        
cout << fixed << setprecision(8) << pi << endl;           // output : 3.14157
       
    // maximum precision of float is upto 6 decimal place
    // while double is beyond this
    // range for int  is    -2x10^(9) to + 2x10^(9)
    // int is        4bytes ( 32bits )
    // float is     4bytes ( 32bits )
    // double is 8bytes (32 bits )

        char ch = 'P';
        cout << ch <<endl; // output : P
            //TypeCasting Example
        cout << (int)ch <<endl; // output : ascii int value of P (66)

        bool isRainy  = true ;
        cout << isRainy << endl; // 1
        bool isSunny = false ;
        cout << isRainy <<endl ; // 0
   
    // 1byte is size of bool
    // 1byte is size of char

2. Type Modifiers
   
    dataType : Char, Int , double 
supports modifiers
   
    // Type modifiers : signed , unsigned , long , short

   
int main (){

    // short int is <= int  in terms of byte( >= 4bytes )
    // long int is >= int in terms of bytes ( >= 4bytes )
    // to know size
        cout<< sizeof(int) << endl ; // system default size of int
        cout << sizeof(long int ) << endl; // system default size of long int
        cout << sizeof(char) <<endl ;       // system default size of char

        int x = INT_MAX ; // gives out the maximum value of int data type in x
        // x + 1 will give us int lowest , i.e in int data type value cycles throug the domain
        // called WrapAround
        // so basically INT_MAX have max int value , while INT_MAX +1 have lowest int value
    
    // signed and unsigned
    // out of 32bits or 64bits first bit is reserved for sign of number ( 0 for + and 1 for - )
    // unsigned makes a data positive and take use of first bit as a magnitude hence extending the size of data in positive domain
        unsigned int x  = -5 // error  or wrong syntax
        

3. More Inits
    // garbage value
       
nothing but the random or default value get stored in variable while declaration

    // More Inits
          
int m1 = 10 // init  10 may not be int we can give 10.5 still works fine
           int m2{10}  // init but {must_integer}
            int m3 = {10} // '=' is redundant ( iska kaam hai hi nhi )

    // typeCasting
        hirrechy of dataType
on operation:
                float>>int>>char>>bool
        example :
            int main () {
                int phy, chem, maths;
                cin >> phy >> chem >> maths;
                int noOfSubject = 3;
                cout<< (float)(phy + chem + maths)/(noOfSubject) <<endl; // will output a float data
               
                int x = 65 ;
                cout << (char)x<<endl; // output : A
               
                char c =  'A';
                cout << (int)c <<endl; // output : 65

                

    // auto Keyword
        c++11 's update
        auto type deduction
           
        
int main () {
            auto marks = 10;
            cout << marks << endl;
            // 'auto' is like 'let' or 'var' keyword in js
            // which default makes marks as a int
            auto c = 'A'  ; // means c is a char keyowrd
            auto b = true ; // means b is bool type
            auto pi = 3.14 ; // means pi is double type

* don't use auto when :
    1. you want to show the type clearly to the user
    2. we want to explicit about variables range or precision
            

        
    // constants
    //
the immutable variables which we want to create once without changing their values
   
    const float pi = 3.14;
    cout << pi << endl;   
    
    // ALITER  by using macro ( less preffered ) ( macro is replacement ways )
    #define pi 3.14;

    

 

4. Scope Resolution Operator
     if defined globally : 
we call it global operator , with global scope
      if defined in block:  we call it block operator , with block scope
     hirearchy of block operator is , the innermost block 's variable dominates if have same name

    

        for specifically calling global use ::

#include<stdio.h>

int x = 20;

int main () {
    int x = 10;
    cout << "local   : "<<x<<endl;    //output :local   : 10
    cout << "global : "<<::x<< endl; //output :global: 20
    return 0;
}

 Previous ( 1.cpp ) << 

 Next (3.cpp ) >>

 

 

 

 

 

 

 

 

 

 

 

 

 

Comments

Popular posts from this blog