Typedef Versus #define In C

Typedef Versus #define In C

Typedef: The typedef Function to provide data type a new name. For example,

// C program to demonstrate typedef 
#include <stdio.h> 
  
// After this line BYTE can be used 
// in place of unsigned char 
typedef unsigned char BYTE; 
  
int main() 
{ 
    BYTE b1, b2; 
    b1 = 'c'; 
    printf("%c ", b1); 
    return 0; 
} 

Output

c

#define in C is referred as a directive that is used to #define alias.



// C program to demonstrate #define 
#include <stdio.h> 
  
// After this line HYD is replaced by 
// "Hyderabad" 
#define HYD "Hyderabad" 
  
int main() 
{ 
    printf("%s ", HYD); 
    return 0; 
} 

Output

Hyderabad

Difference between typedef and #define:

  1. typedef gives names to types, like calling an int a “Score.” #define gives names to values, like saying 3.14 is “PI.”
  2. typedef is handled by the compiler, while #define is managed by a preprocessor before compiling.
  3. #define doesn’t need a semicolon, but typedef does.
  4. #define just swaps text in your code, while typedef creates new types.
  5. With typedef, if you create a new type inside a function, it only works within that function. #define works everywhere in your code.


// C program to demonstrate importance 
// of typedef over #define for data types 
#include <stdio.h> 
typedef char* ptr; 
#define PTR char* 
int main() 
{ 
    ptr a, b, c; 
    PTR x, y, z; 
    printf("sizeof a:%zu\n" ,sizeof(a) ); 
    printf("sizeof b:%zu\n" ,sizeof(b) ); 
    printf("sizeof c:%zu\n" ,sizeof(c) ); 
    printf("sizeof x:%zu\n" ,sizeof(x) ); 
    printf("sizeof y:%zu\n" ,sizeof(y) ); 
    printf("sizeof z:%zu\n" ,sizeof(z) ); 
    return 0; 
} 

Output


sizeof a:8
sizeof b:8
sizeof c:8
sizeof x:8
sizeof y:1
sizeof z:1

The program given above specifies that the size of a is 8 which has the size of the pointer.That is, pointers are stored as 8 bytes. Compiler comes to

typedef char* ptr;
ptr a, b, c;

The statement will change into

char *a, *b, *c;

Further, it will declare a, b, c as char*.

#define will work as the following

#define PTR char*
PTR x, y, z;

The statement will turn into

char *x, y, z;


  • In this situation, you have three things: x, y, and z.
  • x is like a signpost that points to a group of characters (char*), so it takes up as much space as a signpost (the size of a pointer).
  • y and z are like individual characters (char), so they take up much less space, just 1 byte each.

So, x is bigger because it’s a signpost, while y and z are smaller because they are like single characters. This happens because when you use macros with pointers, the first one gets the special treatment of being a pointer, while the others become regular characters.

FAQ- Typedef Versus #define In C

Q1. What is #define used for in C?

Ans. In C, #define is like a tool that helps us create shortcuts, called macros. These macros are like nicknames we give to values. Before the computer actually puts our code together, it replaces these nicknames with the actual values. So, we can use #define to make our code shorter and clearer, especially when we want to name constants or create simple functions

Q2. What is the difference between typedef and struct in C?

Ans.
In C, a struct is like a container that lets us group different types of information under one name. It’s like having a box where we can put variables of all shapes and sizes together.
Now, typedef is like giving a nickname to an existing type. So, we can use typedef to create a shortcut name for our struct or any other data type. It’s like saying, “Hey, instead of calling it ‘struct,’ let’s just call it something simpler, like ‘Person’.”
In summary, struct helps us make a mixed bag of variables, and typedef helps us give a simpler name to our mixed bag.

Q3. What is typedef example?

Ans. typedef is especially handy for giving a simpler name to structures. In your example, it says, “Let’s call this structure ‘person’ instead of saying ‘struct {int age; char *name}.'” And you’re right, when you use person, it becomes a type specifier, not a variable name.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment