Let’s see the short description of the typedef and macro to understand the difference between them.
typedef:
The C language provides a very important keyword typedef for defining a new name for existing types. The typedef is the compiler directive mainly use with user-defined data types (structure, union or enum) to reduce their complexity and increase code readability and portability.
Syntax,
typedef type NewTypeName;
Let’s take an example,
typedef unsigned int UnsignedInt;
Now UnsignedInt is a new type and using it, we can create a variable of unsigned int. So in the below example, Mydata is unsigned int variable.
UnsignedInt Mydata;
Note: A typedef creates synonyms or a new name for existing types it does not create new types.
Macro:
A macro is a pre-processor directive and it replaces the value before compiling the code. One of the major problems with the macro is that there is no type checking. Generally, the macro is used to create the alias, in C language. A macro is also used as a file guard in C and C++.
Syntax,
#define MACRO_NAME MACRO_VALUE
eg,
#define VALUE 10
Now VALUE becomes 10 in your program. You can use the VALUE in place of the 10.
Answer:
Let’s see the short description of the typedef and macro to understand the difference between them.
typedef:
The C language provides a very important keyword typedef for defining a new name for existing types. The typedef is the compiler directive mainly use with user-defined data types (structure, union or enum) to reduce their complexity and increase code readability and portability.
Syntax,
Let’s take an example,
Now UnsignedInt is a new type and using it, we can create a variable of unsigned int. So in the below example, Mydata is unsigned int variable.
Note: A typedef creates synonyms or a new name for existing types it does not create new types.
Macro:
A macro is a pre-processor directive and it replaces the value before compiling the code. One of the major problems with the macro is that there is no type checking. Generally, the macro is used to create the alias, in C language. A macro is also used as a file guard in C and C++.
Syntax,
eg,
Now VALUE becomes 10 in your program. You can use the VALUE in place of the 10.
need an explanation for this answer? contact us directly to get an explanation for this answer