typedef vs #define in C

typedef vs #define in c, you should know

In my previous post, I have written on typedef and its application. I have found that there is a lot of students and fresher that think typedef and #define both are the same things. In this article, I will describe the differences between the typedef and #define in C (typedef vs #define in C).

typedef :

A typedef defines a new name for existing types and does not introduce a new type. It is the (partial storage-class specifier) compiler directive mainly use with user-defined data types (structure, union or enum) to reduce their complexity and increase code readability and portability.

You can check this link to know typedef in detail: typedef in C

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.

#include <stdio.h>
 
typedef unsigned int UnsignedInt;

// After this line UnsignedInt can be used
// in place of unsigned int
 
int main()
{
    UnsignedInt data1, data2;
    
    data1 = 100;
  
    data2 = 200;
  
    printf("%d  %d ",data1,data2);
    
    return 0;
}

Output: 100 200

If you want to learn more about the c language, here 10 Free days C video course for you.

C tutorial

#define

A  #define is a preprocessor directive and it replaces the value before compiling the code. One of the major problems with the macro that there is no type checking. Generally, the macro is used to create the alias, in C language macro is also used as a file guard.

For example,

#define Value 10

 

Now Value becomes 10, in your program, you can use the Value in place of the 10.

#include <stdio.h>
 
// After this line Value is replaced by
// 10
#define Value 10
 
int main()
{
    printf("%d ", Value);
    
    return 0;
}

Output: 10

There are few points that describe how typedef is different from the #define (typedef vs #define in C)

  • The typedef has the advantage that it obeys the scope rules. That means you can use the same name for the different types in different scopes. It can have file scope or block scope in which declare. In other words, #define does not follow the scope rule.

See the below program for better understanding,

#include <stdio.h>


int main(int argc, char *argv[])
{
    // define new type for char *
    typedef char * TypeName;

    // define new type for int
    typedef int TypeName;


    TypeName iData = 4;

    printf("Display Message = %s\n\n",iData);


    return 0;
}

 Output:

Explanation of the code,
In the above code, both created types have the same name and they declare in the same scope. So when we will compile the code, we will get the compiler error.

#include <stdio.h>

int main()
{
#define Value 20

#define Value 10

    printf("%d ", Value);
    return 0;
}

Output: 10

Explanation of the code,
When compiling the above code, we will get a warning “warning: “Value” redefined” but will not get a compiler error.

 

  • typedef is compiler token while #define is a preprocessor token.
  • typedef is ended with semicolon while #define does not terminate with a semicolon.
  • typedef is used to give a new symbolic name to the existing type while #define is used to create an alias of any type and value.

See example code,

#include <stdio.h>

typedef char * CharPtr;

#define  CHAR_PTR char *

int main()
{

    //Declare variable using the #define
    CHAR_PTR Data1, Data2;

    //Declare variable using the new type
    CharPtr Data3, Data4;

    return 0;
}

 

Explanation of the code,
In the above code, when we used CHAR_PTR to declare the variable then Data1 become character pointer and Data2 become character variable. In another case when we used CharPtr to declare the variable then Data3 and Data4 both become character pointer.

Recommended Post