typedef and #define in C program

0

In C programming language, both typedef and #define are used for defining new types, constants or macros, but they serve different purposes.


typedef is used to create a new name for an existing data type. It does not define a new type, but rather an alias for an existing type. For example:


typedef int myint;


This creates a new type myint which is an alias for int. Now, we can use myint instead of int:

myint x = 10;

This is equivalent to:

int x = 10;

On the other hand, #define is a preprocessor directive that allows you to define a constant or a macro. It replaces the defined identifier with its value throughout the code. For example:

#define PI 3.14159

This defines a constant PI with the value 3.14159. Now, we can use PI instead of 3.14159:

double circumference = 2 * PI * radius;

This is equivalent to:

double circumference = 2 * 3.14159 * radius;

#define can also be used to define macros, which are code snippets that can be used as if they were functions. For example:

#define SQUARE(x) ((x) * (x))

This defines a macro SQUARE which takes an argument x and returns its square. Now, we can use SQUARE like this:

int x = 5;
int y = SQUARE(x);

This is equivalent to:

int x = 5;
int y = ((x) * (x));

Difference between typedef and #define


The typedef and #define keywords in C are both used to create aliases or substitute expressions in the code, but they have some fundamental differences:

  • typedef is used to define a new type name, while #define is used to define a value or an expression substitution.
  • typedef can only be used to create a new name for an existing type, while #define can be used to create macros or constants, which can be used in place of expressions, statements, or functions.
  • typedef is a compiler-level feature that is aware of the underlying type, while #define is a preprocessor feature that is not aware of the underlying type.
  • typedef requires a semicolon at the end of the statement, while #define does not require a semicolon.
  • typedef interpretation is performed by the compiler, while #define statements are performed by the preprocessor.
  • #define is essentially a textual substitution performed by the preprocessor, while typedef creates a new type name that can be used throughout the code.
  • typedef follows the scope rule, which means that if a new type is defined in a scope, then the new type name will only be visible within that scope. On the other hand, #define does not follow the scope rule and can be used anywhere in the code after it has been defined.

Here is an example to illustrate the differences:


In the above code, typedef is used to define a new type name myint as an alias for int. On the other hand, #define is used to define a preprocessor macro MYINT as an alias for int.

Inside the function foo(), we can use myint and int interchangeably because they are the same type, but we cannot use MYINT because it is just a preprocessor macro and does not define a new type.


Tags

Post a Comment

0Comments
Post a Comment (0)