Variable Scope:



- static variable address can be used outside its  scope

- static/ variable needs to be initialised using constant

The any local variables hide the globals 

-  No Two storage classes for single variable

typedef static int *i - Error - Two storage classes for i. typedef is also considered as storage class

- int const * ptr - pointer -> const int
- int * const ptr - constant pointer to int

-   register variable can not  be accessed by dereferencing pointer

-------------------------


int fun()
{
  static int num = 16;
  return num--;
}
  
int main()
{
  for(fun(); fun(); fun())
    printf("%d ", fun());
  return 0;

}
Explanation: Since num is static in fun(), the old value of num is preserved for subsequent functions calls. Also, since the statement return num– is postfix, it returns the old value of num, and updates the value for next function call.
-------------------------------

- What if the inner block itself has one variable with the same name?
If an inner block declares a variable with the same name as the variable declared by the outer block, then the visibility of the outer block variable ends at the pint of declaration by inner block



Enums:

- Always they should be assigned integer constatnt values
- Enum can be assigned any value in between, next type will get incremented value of the previous value.

- Enum name should be unique, even across different enum types.


enum state  {working, failed};

enum result {failed, passed};


The C standard specifies that enums are integers, but it does not specify the size. Once again, that is up to the people who write the compiler. On an 8-bit processor, enums can be 16-bits wide. On a 32-bit processor they can be 32-bits wide or more or less.
In normal use you really don’t have to worry about the size of your enums, they will be big enough to do what you want. But when putting them into message to be sent to another system, you should remember that they can change size and may not be what you are expecting. If you store enums in non-volatile memory or on disk, be sure to review any code that might need to be updated when the size changes.


Structures Alignment:

compiler will introduce alignment requirement to every structure. It will be as that of the 

largest member of the structure.

Misaligned Exception: Structure packing is removed, members loose the alignment.

Typically reading byte by byte is an option to avoid misaligned exceptions



A note on malloc() returned pointer

- The pointer returned by malloc() is void *. It can be converted to any data type as per the need of programmer. The implementer of malloc() should return a pointer that is aligned to maximum size of primitive data types (those defined by compiler). It is usually aligned to 8 byte boundary on 32 bit machines.
- Need to care ful when using generic pointer like (void *), as it is not sure that memory is aligned to required data type.
- Array members are deeply copied when a struct variable is assigned to another one.
- A structure variable can be assigned to other using =, but cannot be compared with other using ==
-In C, struct and union types cannot have static members.
-A structure cannot contain a member of its own type because if this is allowed then it becomes impossible for compiler to know size of such struct. Although a pointer of same type can be a member because pointers of all types are of same size and compiler can calculate size of struct
Bit Fields:
- How structure size is determined with bitfields: struct { int a:5, int b:2, int y};
   First 4 bytes are allocated for first member bitfield a, next 2 bits of b can fit in remainting bits 4 bytes,  and  another 4 bytes allocated for y = 4 + 4 = 8 byets
-We cannot have pointers to bit field members as they may not start at a byte boundary
-It is implementation defined to assign an out-of-range value to a bit field member
-Array of bit fields is not allowed. For example, the below program fails in compilation.
- Array parameters are always passed as pointers, even when we use square brackets.
- void pointers cannot be dereferenced. 
-The C standard doesn’t allow pointer arithmetic with void pointers. However, in GNU C it is allowed by considering the size of void is 1


######
MCARO
######
1. undefined macro are initialised to zero
2. Always try to braces around variables for macro expansion
3. macro is not expanded in the string constants
4. it can be used for fucntion names, variable type
5. v1##v2 - merged two tokens
6. macros can be nedsted.






Comments