What does the typedef keyword do?


This keyword provides a short-hand way to write variable declarations. It is not a true data typing mechanism, rather, it is syntactic "sugar coating".
For example

typedef struct node
{
  int value;
  struct node *next;
}mynode;

This can later be used to declare variables like this

mynode *ptr1;
and not by the lengthy expression
struct node *ptr1;

There are three main reasons for using typedefs:
    • It makes the writing of complicated declarations a lot easier. This helps in eliminating a lot of clutter in the code.
    • It helps in achieving portability in programs. That is, if we use typedefs for data types that are machine dependent, only the typedefs need to change when the program is ported to a new platform.
    • It helps in providing better documentation for a program. For example, a node of a doubly linked list is better understood as ptrToList than just a pointer to a complicated structure.


And also read
1.    Do Global variables start out as zero? 
2.    Does C have boolean variable type?
3.    Where may variables be defined in C?
4.    To what does the term storage class refer? What are auto, static, extern, volatile, const classes?
5.    What does the typedef keyword do?
6.    What is the difference between constants defined through #define and the constant keyword?
7.    What are Trigraph characters?
8.    How are floating point numbers stored? Whats the IEEE format?
9.    When should a type cast be used?
10. Can structures be assigned to variables and passed to and from functions?

 


No comments:

Post a Comment