+ 2
When we use typedef and enum in c language?
2 Answers
+ 28
Enumeration is a user defined datatype which provides a way for attaching names to numbers, and "enum" keyword is used to declare an enumeration.
(It also makes a program easy to read and maintain )
For example :-
enum colour{ red, blue, green }
By default enum assigns integer starting with 0 for first enum, 1 for second and so on... .
We can over-ride this by explicitly assigning integer value to enum , like
1. enum colour{ red, blue = 4, green = 8}
2. enum colour{ red = 5 , blue , green }
In first red is 0 by default and in second blue is 6 and green is 7.
+ 26
1. typedef is a keyword, use to tell the compiler for assigning an alternative name to already exist data types.
( This is use to prevent us from writing more and also makes the code clearer and easy to understand )
For example :-
typedef signed long sl;
Now 'sl' becomes user-defined identifier and can be implemented in your program for defining any signed long variable type within your program. This means:
sl a , b;
will allow you to create two variables name 'a' and 'b' which will be of type signed long and this quality of signed long is getting detected from the sl (typedef), which already defined the meaning of sl in your program.