Thursday, February 7, 2019

C++ Qualifiers and Macros

type qualifier: const vs volatile
There are two types of qualifiers, const or volatile, then there is mutable.
Type qualifier is also known as CV qualifier, where CV stands for constant and volatile.
Type qualifiers express additional information (quality) about a value through the type system.

const
const marks a variable as read-only or immutable. Its value cannot be changed once it's been defined.

volatile
volatile marks a variable that may be changed by another process. This is generally used for threaded code.
The keyword volatile, which is rarely used, creates variables that can be modified not only by the program
but also by other programs and external events. Events can be initiated by interrupts or by a hardware clock.

mutable
And mutable is used on a data member to make it writable from a const qualified member function.
Examples
    // const
    // volatile
    // mutable
    
    // qualified type : constant integer
    const int cint = 0;
    
    // unqualified type : simply an integer
    int unqualified_int = 0;
    
    // mutable with lambda
    int accum = 9;
    auto x = [accum](int d) mutable -> int { return accum += d; };
    vector v1 = { 1,2,3,4,5 };
    vector v2 (v2.size());
    transform (v1.begin(), v1.end(), v2.begin(), x);
    
    Storage Duration
    static
    register
    extern
    
    Modifiers
    signed
    unsigned
    
    Operator
    sizeof(type) // byte size of data type
    
    Integer Constants
    printf ( "binary %u, decimal %d, %d, octal %o, hex %x\n", 
              0b10000, 0b10000, 16, 020, 0x10 );
    
    //------------------------------------------------------------------------------
    // Decimal      Octal           Hexadecimal             Type
    //------------------------------------------------------------------------------
    // 16           020             0x10                    int
    // 255          0377            0Xff                    int
    // 32768U       0100000U        0x8000U                 uint
    // 10L          012L            0xaL                    long
    // 27UL         033UL           0x1bUL                  unsigned long
    //
    // Binary: 0b110 (decimal 6)
    //------------------------------------------------------------------------------
    
    
    Floating-point Constants
    5.19            0.519E1         0.0519e2        519.OE-2
    12.             12.0            .12E+2          12e0
    0.75            .75             7.5e-1          75E-2
    0.00004         0.4e-4          .4E-4           4E-5
    
    Character Constants
    //------------------------------------------------------------------------------
    // Constant             Character               Constant Value (ASCII code)
    //------------------------------------------------------------------------------
    'A'                     A                               65
    'a'                     a                               97
    ' '                     blank                           32
    '.'                     dot                             46
    '0'                     digit 0                         48
    '\0'                    terminating null character      0
    //------------------------------------------------------------------------------
    
    // String "0" comprises two bytes, 
    // first byte contains the code for the character zero '0' (ASCII code 48)
    // second byte contains the value 0 ('\0')
    
    //------------------------------------------------------------------------------
    // Special Characters:
    \a      alert (BEL)                     7
    \b      backspace (BS)                  8
    \t      horizontal tab (HT)             9
    \n      line feed (LF)                  10
    \v      vertical tab (VT)               11
    \f      form feed (FF)                  12
    \r      carriage return (CR)            13
    \"      " (double quote)                34
    \'      ' (single quote)                39
    \?      ? (question mark)               63
    \\      \ (backslash)                   92
    \0      string terminating character    0
    //------------------------------------------------------------------------------
    \ooo (up to 3 octal digits)  numerical value of a character    ooo (octal)
    \xhh (hex digits)            numerical value of a character    hh (hex)
    //------------------------------------------------------------------------------
    
    
    MACROS
    #define CLEARSCREEN  ( cout << "\033[2J" )
    #define LOCATE(x,y)  ( cout << "\033[" << x << ';' << y << 'H' )
                         // LOCATE will position cursor in row x column y
    #define SQUARE(x)    ( (x)*(x) )
    
    #define PI           3.14159
    #define UPPERBOUND   10
    #define LOWERBOUND   (-3)
    #define PRINTHDR     ( cout << \
                           " ***** REPORT *****\n\n" )
    
    int main()
    {
        PRINTHDR;
    }
    
    #define MIN(x,y)     ( (x)<(y)? (x) : (y))
    // ... use MIN 
    #undef MIN
    // cannot use MIN any more
    
    #ifdef _NAME_
    #endif
    #ifndef _NAME_
    #endif
    
    #ifndef _FILE_NAME_H_
    #define _FILE_NAME_H_
    #endif
    

    No comments:

    Post a Comment