Wednesday, February 6, 2019

C++ literals!

Numbers, Strings, and Boolean
#include <iostream>
#include <cstdio>
#include <cstdint>

int main()
{
    constexpr size_t byte = 8;

    bool b;
    int i = 5;
    float f;
    int x[5] = { 1,2,3,4,5 };
    char s[] = "this is a string";

    short int sh;
    long int  li = 0L;
    long long int lli = 0LL;

    // integer types
    char iic;                   // 8-bit
    short int iis;              // 16-bit
    int iii;                    // 32-bit
    long int iil;               // 32-bit or 64-bit
    long long int iill;         // double size of long int
    cout << "char size "  << sizeof(char) << endl;
    cout << "short ing size " << sizeof(short int) << endl;
    cout << "int size "   << sizeof(int) << endl;
    cout << "long int size " << sizeof(long int) << endl;
    cout << "long long int size " << sizeof(long long int) << endl;
    cout << sizeof(iic) * byte << endl;
    cout << sizeof(iis) * byte << endl;
    cout << sizeof(iii) * byte << endl;
    cout << sizeof(iil) * byte << endl;
    cout << sizeof(iill) * byte << endl;

    unsigned char uic;                  // 8-bit
    unsigned short int uis;             // 16-bit
    unsigned int uii;                   // 32-bit
    // long int can be 32 bit(pc) or 64 bit(mac) depends on compiler library.
    // they are guaranteed to be in relationship to each other 
    // but not guaranteed to be particular sizes
    unsigned long int uil;              // 32-bit or 64-bit
    unsigned long long int uill;        // double size of long int

    uint8_t  iax;                       // unsigned
    int16_t  iay;                       // signed
    uint32_t iaz;                       // unsigned
    int64_t  iap;                       // signed
    uint64_t iaq;                       // unsigned

    wchar_t wct;                        // 4 byte, wide character type
    size_t  szt;                        // 8 byte

    float ff;
    double df;
    long double ldf;
    cout << "sizeof float " << sizeof(ff) << endl;
    cout << "sizeof double " << sizeof(df) << endl;
    cout << "sizeof long double " << sizeof(ldf) << endl;

    printf ( "print .3 equal .1+.1+.1 %s\n", 
        ( .3 == .1+.1+.1 )? "true" : "false" );
}
Of course strong typed!
#include <cstdio>
#include <iostream>
using namespace std;

int main(void)
{
    int ia[] = { 1,2,3,4,5 };
    int x , y , z = 0;
    auto j = 3;
    for( auto i : ia ) {
        printf( "i is %d\n", i );
    }
    string str = "C++ Language";
    const char *cstr = str.c_str();
    printf ("Hello %s\n", cstr);
    cout << "Hello" << str << endl;

    char mystr[] = " more and more";
    printf( "hello " " \u03bc" " xxx %s" "\x40" "\n", mystr ); // \x40 is @ sign

}
Pointers and References
    // references, it is an alias
    int ix = 42;
    int & yx = ix;

    // pointer
    int * ipx = &i;
    printf ("value is %d\n", *ipx );
Struct and Union
    struct x {
        int a;
        float b;
        char c[10];
    };

// A union is a set of overlapping objects. This allows a single compound object 
// to hold objects of different types at different times 
// overlapping the same memory space.

    union y {
        int a;
        float b;
        char c[10];
    };
Compiler Switches
% c++ -Wall -std=c++11 -std=c++14 -std=c++17 <file.cpp>

# For CodeLite
# Right click the project name
# Go to the bottom and select [Settings...]
# Select C++ Compiler Options 
#      and on the far right, [...] will show up
# Click [...] on the right and add options for 
#      -std=c++11, -std=c++14, -std=c++17

No comments:

Post a Comment