Coding Tips

Create your own data type

 Programmer defined data types are one of the most powerful features offered by any programming language. They are easy to handle in situations of unforeseen changes.

Following is one such situation which explains the benefit of using user defined data types.

In C programming language, user can define their own data types using typedef


               typedef float coordinate;

Here, coordinate is a user defined data type which can be used to declare variables of type float as shown in the below function;

               function1( )
               { 
                   coordinate n1,n2;
                   coordinate result;
                     ........................
                }

Now suppose we want to change the data type of all float variables to double, it is enough to change it in typedef as follows:

                typedef double coordinate;

Hence it is advisable to create user defined data types whenever it is possible for the following reasons:
  • To make modification easier
  • To avoid excessive information distribution
  • To increase reliability
  • To make-up for programming language weaknesses

No comments:

Post a Comment