Skip to main content

Typedef vs Hash Define in C

Typedef vs Hash Define

Both typedef and #define (or "hash defined") are used to create aliases for existing types or values.

Typedef

  • typedef is a keyword used to create a new name for an existing type, making it easier to use and read in code.
  • The syntax for typedef is as follows:
typedef <existing_type> <new_name>;
  • We can use typedef to create a new name for the int type like this:
typedef int my_int;
  • This creates a new type called my_int that is equivalent to int. We can now use my_int in our code instead of int:
my_int x = 42;

The #define directive

  • The #define directive, on the other hand, is used to define a macro that can replace a particular value or piece of code in the codebase.
  • The syntax for #define is as follows:
#define <macro_name> <macro_value>
  • We can use #define to define a macro that replaces the constant PI with its numerical value:
#define PI 3.14159
  • This creates a macro called PI that can be used to replace 3.14159 anywhere in the code.

For example:

double circumference = 2 * PI * radius;

Examples

Below Examples show how we can use typedef and #define interchangeably to achieve the same purpose - defining an alias for a struct representing a complex number.

One uses typedef and the other uses #define.

Example using typedef:

#include <stdio.h>

typedef struct
{
double real;
double imag;
} Complex;

void print_complex(Complex c)
{
printf("%.2f + %.2fi\n", c.real, c.imag);
}

int main()
{
Complex c = {3.0, 4.0};
print_complex(c);
return 0;
}
Output:

Example using #define:

#include <stdio.h>

#define Complex \
struct \
{ \
double real; \
double imag; \
}

void print_complex(Complex c)
{
printf("%.2f + %.2fi\n", c.real, c.imag);
}

int main()
{
Complex c = {3.0, 4.0};
print_complex(c);
return 0;
}
Output:
  • Both programs define a struct representing a complex number with two fields for the real and imaginary components.
  • They also define a function to print a complex number, which takes a parameter of the struct type.
  • The main difference between the two programs is in how they define the alias for the struct type.
  • The first program uses typedef to create a new type name Complex that is an alias for the struct type.
  • The second program uses #define to create a text replacement for the struct definition.

Typedef vs Hash Define - Similarities

Although typedef and #define serve different purposes, they do share some similarities, such as:

  • Both are used to create aliases for existing types or values in C.
  • They allow for easier and more readable code by providing a more meaningful or shorter name for an existing type or value.
  • They can be used to define custom types or constants for use throughout the codebase.
  • They both modify the way code is interpreted by the preprocessor, but do not generate executable code on their own.
  • They are often used in header files to provide a common interface for multiple source files to use.
  • They can be used to improve portability of code, as they allow for easier updates and changes to types or values.
  • Both are subject to name clashes if not used carefully, which can lead to unexpected behavior in the code.

Typedef vs Hash Define - Differences

Below table summarized the key differences between typedef and #define

typedef#define
PurposeCreate new type namesCreate text replacements
ScopeLimited to typesCan be used for any value or expression
Syntaxtypedef "existing_type" "new_type"#define "name value"
Type safetyImproves type safety and readabilityCan sometimes obscure code meaning
Modify typesCannot modify existing typesCan modify existing values or expressions
Anonymous typesCan create anonymous typesCannot create anonymous text replacements
Preprocessor Not handled by the preprocessorHandled by the preprocessor
Keyword A keyword in CA preprocessor directive