Wednesday, August 12, 2015

Structures

While reading in the book "The complete C Reference". I got some notes!


Structures, Unions and Enumerations are declared/defined similar to each other

  • struct tag{   type member_name;
       type member_name;   type member_name;
    } variable_list;
  • union tag{   type member_name;
       type member_name;   type member_name;
    } variable_list;
  • Enumeration tag{   enumeration list
    } variable_list;
Where, tag is optional, variable list is optional, but at least one of them must exist

  • For bit fields, the bits runs from left to right or from right to left dependent on the machine
  • typedef syntax:
    typedef type_name new_name;

Wednesday, August 5, 2015

Expressions

While reading in the book "The complete C Reference". I got some notes!

Expressions

  • DataTypes:
    • char: 1 byte
    • int
    • float
    • double
    • void
  • Modifiers:
    • signed
    • unsigned
    • long 
    • short
  • DataTypes with modifiers (some notes)
    • the default is "signed"
      Ex:
      • char = signed char
    • Sizes:
      • char: 8 bits
      • short int: 16 bits
      • long int: 32 bits
      • long long int: 64 bits (added by C99)
      • float: 32 bits
      • double: 64 bits 
      • long double: 80 bits (added by C99)
  • Identifiers (variable and functions names, labels, ...etc) :
    • first character: "_" or a letter
    • other characters: "_" or letter or number
    • significant charachters:
      • internal identifiers (used only in the same file)
      • external identifiers (used in more than one file, like global variables and functions)
      • C89:
        • for internal identifiers: 31 characters are significant
        • for external identifiers: 6 characters are significant ("studentName" and "studentNameCon" will be treated as the same identifier!)
      • C99:
        • for internal identifiers: 63 characters are significant
        • for external identifiers: 31 characters are significant
  • Variables
    • C89: all variables shall be declared in the beginning of the block
    • C99: can be defined anyway
  • 4 C Scopes:
    • File Scope (variables defined in file scope are global)
    • Block Scope (variables defined in a block scope, are local to there block, also variables defined in the function definition (formal parameters) are local to the function block scope )
    • Prototype Scope (variables declared in function prototype, are local to the prototype)
    • Function Scope: applies only to the labels. (so block scope does not apply to labels, function scope is applied instead!)
      • the following code is not valid
        void func1(){
         
        fun1: goto fun2;
        }
        void fun2(){
         
        fun2: goto fun1;
        }
      • the following code is valid
        void fun1(){
         printf("fun1");
            {
               
        block1: printf("block1");
            }
            goto
        block1;
        }
  • Type Qualifiers:
    • const
      • Saved in ROM
      • The program can not change this variable
      • Other component other than the program can change the variable (ex: hardware device)
    • volatile
      • To highlight that this variable may be changed, without explicit assignment in the program
        ex:
        may be changed by HW device, by operating system, ...etc
      • This prevents compiler optimization like the following
        • y = x*3/(5x+2);    =======>     y = x*3/(5x+2);
          z = x*3/(5x+2);                 z = y;
  • Storage Class Specifiers:
    • extern  ==> the variabale is defined in other file
      • In some compilers, it is not mandatory to use "extern" key word
    • static
      • in file scope ==> the variable is file global (internal)
      • in block scope ==> the variable shall be initialized only once at the program startup
    • register
      • This variable shall be stored in a register if applicable (faster access)
      • If no registers are available, define them some how to be faster in operations.
      • Allowed for local variables.
      • Theoretically, can be ignored by the compiler if not possible, but this seldom happens.
    • auto
      • The variable is local to the function (no need to write this! all function variables are auto by default)
  • Constants
    • The compiler fits numeric constants to data types as follows:
      • Default fit
        • Integer constant: to the smallest data type fits
          • 16 >> short
          • 100000 >> long
        • Floating point constant: to double
          • 16.3 >> double
      • postfix fit
        • 12.9F or 12.9f >> float
        • 12.9L or 12.9l >> long double
        • 12U or 12u >> unsiged short
        • 12L or 12l >> long int
    • Hexadecimal and Octal
      • 0x10 ==> Hexadecimal
      • 010 ==> Octal
  • Operators:
    • Assignment operator "="
      • Multiple Assignment
        • x = z = 1;
      • Compound assignment
        • x += 1;
    • Arithmatic operators
      • ++ , --
      • - (unary operator)
      • + , / , %
      • + , - 
    • Relational and logical operators
      • !
      • > , >= , < , <=
      • == , !=
      • &&
      • ||
    • Bitwise Operators
      • & , | , ^ , ~
    • Other Operators
      • ? (ternary operator)
      • pointers: * , &
        • pointer operators and unary "-" operator have the highest precedence
    • sizeof()
      • unary operator
      • run time operator
      • returns the size of the operand (in bytes)
      • the return value is of type size_t, where:
        "size_t" is defined to be "unsigned int", using typedef
    • Comma operator
      • separates expressions
      • the result is the last expression result
      • x = (y = 5, y + 1);
        this means:
        • put y = 5
        • then put x = y + 1
    • dot (.) and  arrow (->)
      • dot (.)
        • for struct/union member reference using the struct
      • arrow (->)
        • for struct/union member reference using the struct pointer
    • () and []
      • ()
        • for precedence
      • []
        • for array indexing
    • Precedence
      • Unary operators and ?
        • associate from right to left
      • Other operators
        • associate from left to right
      • Precedence:
        • ()    []    ->    . 
        • !    ~    ++    --    -    (type)    *:at address    &:address of    sizeof
        • *    /    %
        • +    -    
        • <<    >>
        • <    <=    >    >=
        • ==    !=
        • &
        • ^
        • |
        • &&
        • ||
        • ?    :
        • =    +=    -=    *=    /=    etc
        • ,
    • Type promotion in expressions:
      • char,  short ==> int
      • if one operand is long double ==> the other is promoted to long double
      • else if one operand is double ==> the other is promoted to double
      • else if one operand is float ==> the other is promoted to float
      • else if one operand is unsigned long ==> the other is promoted to unsigned long
      • else if one operand is long ==> the other is promoted to long
      • else if one operand is unsigned int ==> the other is promoted to unsigned int
    • Example: 
      • unsigned char x = 0xFF
      • if (~x == 0x00) // evaluates to false
        • because x will be promoted to unsigned int -> 0x000000FF
        • ~x = 0xFFFFFF00