Tuesday, 5 June 2018

Typedef



typedef is a reserved keyword in the C and C++ programming languages. It is used to create an alias name for another data type.



Limitations :-


  • typedef is limited to giving symbolic names to types only, where as #define can be used to define alias for values as well, e.g., you can define 1 as ONE, 3.14 as PI, etc.

  • define should not be terminated with semicolon, but typedef should be terminated with semicolon.

  • typedef follows the scope rule which means if a new type is defined in a scope (inside a function), then the new type name will only be visible till the scope is there.




  • Enum

    An enum is a keyword, it is an user defined data type. All properties of integer are apples to Enum. It is used for creating an user defined data type of integer. Using enum we can create sequence of integer constant value.

    Key Points :


    • With the help of enumeration we can put a name for constant.
    • User readability improves;
    • Default first member value is 0(zero)
    • We cannot increment or decrement.
    Syntax :

    enum { red, black = 5, white, green };


    Union

    A union is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes.

    Key Points :

    • It is an user defined data type;
    • Union is a collection of different type of memory but not in contiguous form;
    • In union all member will share same location;
    • Size of union depends upon the size of the largest member;
    Syntax :

               union tagname 
                          {
                            member 1;
                            member 2;
                            member 3;
                            .
                            .
                            .
                           };

    STRUCTURE - 3 (Structure padding)


                         

                  "Allocating the memory for a structure variable more then the required memory is called structure padding, that extra byte of memory is called hole in a structure;"






    Some more examples:

    struct A
             {
             int a;
             char c;
             float f;
             };
    Size of struct A should be 9 but it is 12, that is known as structure padding and extra 3 byte are known as hole.

    For removing the hole we can use below keyword:

    #pragma pack(1)   //only in GCC compiler 



    STRUCTURE - 2

    Now we discuss some key features of structure :- 

    Structure Pointer :

                    Pointer size is dependent upon OS ,same applies for structure pointer. we will assume a 32-bit system.

    Exp :

    struct st
             {
              int a;
              char b;
              };
    main()
    {
    struct st a1 ={10, p} , *ST;
    ST = &a1;

    for access :-

    printf("%d %c",ST->a , ST->b );

    Ways to Pass a structure in function :

    1) print1(a1.a, a1.b);
    2) print2(ST);
    3) print3(&a1);

    Structure Bit Field :

    •  Bit field is a mechanism in C-Language where we can allocate a memory in the form of  Bits.
    • Bit field can only be used in Structure and Union.
    struct A
             {
             int a:6;
             chat ch:3;
             };

    Here int take b bit and char take only 3;

    Limitation of bit Field :
    • We cannot allocate more bit size, then the actual size.
    • bit field are applicable only for char and int.
    • Getting address of bit field variable is possible.
      

    Monday, 4 June 2018

    STRUCTURE - 1

    Structure is an user define data type which is collection of different primitive data types.

    Syntax :

    struct tagname
           {
           member 1;
           member 2;
           member 3;
           .
           .
           .
           };



    • tagname is nothing but new data type;
    • Until and unless we create a variable for structure there is no memory allocated for the member;
    • If we want to access the member of structure through structure variable we use dot(.) operator;
    • If we want to access the member of structure through structure pointer, we use arrow (->) operator;

    Limitation of structure :-

    • It is not possible to initialize the member of structure.
    • Storage classes are not allowed for structure members but storage classes are allowed for variable of structure.
    • In a structure we can't declare the same structure type but we can create a same structure pointer. (Self referential structure)
    • Structure within structure is possible but the changed name (using typedef) structure is not possible in that structure.

    User Defined Data Type

    Sometimes, the basic set of data types defined in the C language such as int, float, char, double etc. may not be insufficient for our program. Under such circumstances we can create our own data types based on primary data types


    There are mechanisms for doing this in C:

    Using : typedef

    Using : Stucture
    Using : Union 
    Using : Enum

    They are all known as User as well as Member define data type because user can amend according to their requirement.


    Use Of typedef :-
    The purpose of typedef is to assign alternative names/ alias name to an existing types. Sometime standard declaration is confusing, or likely to vary from one implementation to another.

    Use Of Structure :-
    Structure is data type available in C that allows to combine data items of different kinds. To define a structure, you must use the struct statement. Structure is a collection of variables of different types under a single name.

    Use Of Union :-
    A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. 

    Use Of Enum :-
    Enumeration (or enum) in C. Enum is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain. enum State {Working = 1, Failed = 0};


    Note: Next we will discuss about all in detail