Thursday, July 9, 2009

What is different between 'typedef' and 'define' in programing of C?

as we use typedef when we use structure: typedef struct node N


and we use define at the above of program for defining

What is different between 'typedef' and 'define' in programing of C?
Well.. a define statement will look like:


#define N struct node





whereas, typedef statement will be:


typedef struct node N;





You would know that #define is a preprocessor instruction while typedefs are handled by C compiler.





Compiler checks if the first parameter to typedef is actually a valid data-type (or another typedef). So you will get correct error message at the location of typedef declaration itself.





Errors while using #defines will be quite confusing as those will be catched when the replacement variable (in this case "N") will actually be used.
Reply:The following forum discussion should get you what you want.
Reply:#define is used to define any value which you are going to use frequently %26amp; which is not going to change. On the other hand typedef is used to give name to any type. Like you can use typedef int number. In higher languages typedef is used frequently to give meaningful names to data types they use. I think there is no confusion now.
Reply:The typedef keyword is used to create a new type. The #define keyword is used to create a macro.





There are many differences. Possibly the most important is that #define defined a textual substitution which is done *before* the code is compiled, for example, if you have:





#define TEST(x, y) call_test(x, y)





then when you compile:





TEST("a", 32);





it will be replaced by





call_test("a", 32);





The above example is simple and not very useful, but much more complex constructions can be used to 'write' boilerplate code for you, such as:





#define TYPE_ARITHMETIC(_type) \


_type _type##add(_type a, _type b) { return a + b; } \


_type _type##subtract(_type a, _type b) { return a - b; }





(The example creates functions to perform addition and subtraction of a given type. For example, suppose you called


TYPE_ARITHMETIC(uint32), it would create two functions, uint32add(uint32 a, uint32 b) and uint32subtract(uint32 a, uint32 b) - notice also that the whole macro substitution needs to expand to a single line, which is the reason for the '\' line terminations)





The problem is that because the text is substituted before compilation, it can be hard to find errors.





typedef creates a type synonym, which can be useful in helping code to match your problem domain, or for dealing with differences between platforms, for example:





typedef unsigned int int32;





creates a new type (int32) which is the same as 'unsigned int'. Again, this seems not so useful, but it can be helpful in more complex declarations, for example:





typedef (*CallbackFunction)(int, void*);





Defines a new type, 'CallbackFunction', which is a much easier declaration to use and understand.





The key thing to understand is that #define performs actions which are substituted in your source code before it is compiled.


No comments:

Post a Comment