Tips - C++

Tips - C++
td_back1 Tips for C/C++
 
 
Reserved C(bold)/C++ Key Words
asm auto bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum extern false float for friend goto if inline int long mutable namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try typedef typeid union unsigned virtual void volatile wchar_t while

The maximum length of a variable name: 31 characters.
C Data Types


Key word Bytes Range
char 1 -128 ~ 127
int 2( or 4) -32768 ~ 32767
short 2( or 4) -32768 ~ 32767
long 4 -2147483648 ~ 2147483647
unsigned char 1 0 ~ 255
unsigned int 2( or 4) 0 ~ 65535
unsigned short 2( or 4) 0 ~ 65535
unsigned long 4 0 ~ 4294967295
float 4 1.2E-38 ~ 3.4E38
double 8 2.2E-308 ~ 1.8E308

Operator Precedence

Arithmetic Operator Relative Precedence
++ -- 1
* / % 2
+ - 3
Relational Operator Relative Precedence
< <= > >= 1
!= == 2

printf conversion specifiers

Specifier Meaning Conversion Type
%c single character char
%d signed decimal integer int, short
%ld signed long decimal integer long
%f decimal floating-point number float, double   (eg: %.2f ->  32.00)
%s character string char array
%u unsigned decimal integer unsigned int, unsigned short
%lu unsigned long decimal integer unsigned long
%x %X unsigned hexadecimal (x:lowercase, X:uppercase)
%o unsigned octal notation /
%% Displays '%' character itself. /
%* Specifies field width.
eg) printf("%*d", 0x0a, value); << The value is printed in field width 0x0a.
/
%e %E Scientific notation of float or double. If no specific precision specifier has been specified with F, it presents maximum 6 digits on the right of the floating point.
eg) 12.3 is presented as 1.230000e + 001
float, double(lowercase/uppercase)
  • Precision specifier(.n where n is a numeric constant) is applied only to the following conversion characters: e, E, f, g, G, s. If used with 's' it specifies the number of characters to be printed, otherwise the number of digits on the right of the floating-point.

  • 'l' modifier can be placed before the following conversion specifiers: o, u, x, X, i, d , b. It specifies long, not int.

  • 'l' modifier can be placed before the following conversion specifiers also: e, E, f, g. It specifies double.


printf conversion flag following '%' modifier

Flag Meaning
- Field output in left-alignment.
+ Preface numbers with '+' or '-' sign.
' ' Insert a space before a number.
# Applicable only to 'x', 'X' and 'o'. Preface a number that is not zero with '0x' or '0X' or '0' respectively.

Initializing multi-dimensional arrays


int array[2][3] = {1, 2, 3, 4, 5, 6};
is the same as the following:
int array[2][3] = {{1, 2, 3}, {4, 5, 6}};

Suspending operations for specified seconds


    #include <time.h>
    void sleep(int iseconds)
    {
        clock_t duration;
        duration = (iseconds * CLOCKS_PER_SEC) + clock();
        while(duration > colck()) ;
    }

struct definition/declaration/initialization and ...

    struct mystruct
    {
        int a;
        int b;
    } one, two;


    struct mystruct
    {
        int a;
        int b;
    } one = {3, 32};


    struct shozi
    {
        struct mystruct numbers;
        char hiraizumi[0x20];
        char kanoya[0x30];
        char rumoi[0x30];
        int value;
        float total;
    } one = {
                {0x04, 0x07},
                "Hiraizumi",
                "Kanoya",
                "Rumoi",
                235,
                6344.323
            };


union common
{
    char c;
    int i;
    float f;
    long L;
};

union common uione = {'A'};

    << Union: initializing only the first member is enough as shown above.


    typedef union _common
    {
        char c;
        int i;
        float f;
        long L;
    } common;

    common uione, uitwo;

    << Using typedef so as not to use union keyword for union variable declarations.

Sample Illustration
Multiple Indirection :
    int n = 0x0a;
    int *pi = &n;
    int **ppi = &pi;
    **ppi = 0xff;//Makes sense
Other

    int multi[0x03][0x04] = {
        {0x01, 0x02, 0x03, 0x04},
        {0x05, 0x06, 0x07, 0x08},
        {0x09, 0x0a, 0x0b, 0x0c}
    };
    // three lines and four columns.
    // multi and multi[index] is a pointer.

    int a = sizeof(multi);
    int b = sizeof(multi[0x00]);
    int c = sizeof(multi[0x00][0x00]);
    // c is the size of int, ie, 4 or 2.
    // b is the size of c * 0x04, ie 16 or 8.
    // a is the size of c * b, ie 64 or 16.

    int (*pi)[0x04];
    // A pointer that can point to an element of multi.

    int *pis[0x04];
    // An array of four int pointers
    // each of which can point to an int variable respectively.

    pi = multi;// Makes sense.
    pis = multi;// Wrong indirection;


    #include <math.h>

    double ceil(double x);
    ceil(4.5) returns 5.0
    ceil(-4.5) returns -4.0

    double floor(double x);
    floor(4.5) returns 4.0
    floor(-4.5) returns -5.0

Dont list
  • Do not put extended ASCII character value in signed char variable.

  • Avoid defining bit fields of eight or sixteen bits in size. (the same size as char or short type)

Mind list
  • Character sets supported by computer systems may vary but most have the same character values in the range from zero to one hundred twenty seven.

  • Bit field members must precede non-bit field members in struct definition.

    
        struct mystruct
        {
            unsigned tagged:0x01;
            unsigned bogged:0x01;
    
            char name[0x20];
            char street[0x50];
        };
    

Just O-shi-ra-se.
  • EOF is defined as -0x01 in stdio.h.

No comments:

Post a Comment