Try answering these questions first; some of the answers might surprise you!
Does typedejmean 'type declaration' or 'type definition'?

Why do we need typedej when we can directly declare variables with specific types? Does typedejhelp in increasing portability of the code? Is typedej a storage class (like static, extern etc)? e Why is that languages like Java do not have typedej or an equivalent feature?

1. typedej stands for 'type definition'; however, it's a misnQmer.typedejnever 'defines' a type, it just declares a type. What does This mean? A definition is always associated with a space allocated for it, while a desJaration is just about giving information to the cpmpiler (think of the difference between a function'dedaration and a definition, for example). When we use a typedej, say, typedejint INT, we declare [NT to be of type int; there is no
space allocated for INT and this typedej details are lost when the code gets compiled. So, typedejs are always declarations.

2. The main use of typedej is in abstracting the type details. Consider the example of FILE* that we use for I/O-we use it without knowing anything about the underlying struct and that it's a typedej!

Name:  Understanding 'typdef' in C.jpg
Views: 160
Size:  61.9 KB

The detail behind FILE is abstracted and the user uses FILE freely as if it is a datatype. So typedejs are useful in simplifying the use of complex declarations.

3. C is a low-level language and its programs can have many details that are implementation dependent. An important benefit in using typedef is that it increases the portability of programs. Consider the prototype of strlen: size_t strlen(const char *);. The size_t is a typedef of unsigned int or unsigned long depending on the platform; so we can use the program that uses strlen in another platform without worrying if the actual return type is unsigned long or unsigned into many details that are implementation dependent.

An important benefit in using typedef is that it increases the portability of programs. Consider the prototype of strlen: size_t strlen(const char *);. The size_t is a typedef of unsigned int or unsigned long depending on the platform; so we can use the program that uses strlen in another platform without worrying if the actual return type is unsigned long or unsigned into many details that are implementation dependent.

3. Yes. The grammar for C specifies: So, typedejis also considered as a st;,orage class specifier! Initially, this question may not make any sense at all because we know that typedejis concerned with types and it has nothing to do with storage classes. For those who don't know what a storage class is: a storage class specifies how/where to allocate space for the variables; for example, the auto storage class specifies that the variable is local to a function and should be created and destroyed as and when a 'function call is made and returned.

What is typedejto do with storage classes? Storage classes such as static, extern, etc, cannot occur together; also, storage classes cannot be type-defined (a typedejspecifies a type and not how/where it is allocated). So the creators of the language designed the granimar of the language using this insight and treated typedej as a storage class (as 'syntactic convenience')!

4. C is a low-level language and, hence, for portability and abstraction, typedefs are very useful. Languages like Java have alternative and better abstraction facilities (we can declare a FILE class, for example); also, higher-level languages are often more portable than C. So, most of the higher-level languages do not need a typedef like feature!