Linux C programming tutorial Part 28 - Typedefs

In this tutorial - which is part of the ongoing C programming tutorial series - we will discuss the concept of typedef. As the name suggests (think of typedef as type+def), typedef is a C provided facility to define new names for existing data types.

Typedef in C Programming Language

Here's an example showing how typedef can be used:

typedef int my_int

The line above - which begins with the keyword 'typedef' - gives datatype 'int' a new name: 'my_int'. So, a code like this:

#include <stdio.h>

int main()
{
int a = 10;
printf("The value of a is: %d", a);

return 0;
}

Can be written like the following when using the typedef example we just discussed:

#include <stdio.h>

typedef int my_int;

int main()
{
my_int a = 10;
printf("The value of a is: %d", a);

return 0;
}

Now, one question that some may ask is what's the difference between a typedef and a #define? Well, while both appear to do similar stuff, there are in fact quite a few differences between the two. 

First of all, typedef is interpreted by the compiler, whereas a #define never makes it up to the compiling stage as it gets interpreted at the preprocessing stage itself. Then, a typedef follows scope rules, whereas a #define doesn't. Following is an example of this:

#include <stdio.h>

void my_func();

int main()
{
typedef int my_int;
my_int a = 10;
printf("The value of a is: %d", a);
my_func();

return 0;
}

void my_func()
{
my_int b = 20;
printf("The value of a is: %d", b);

return;
}

When this code is executed, you get the following error:

main.c: In function ‘my_func’:
main.c:25:5: error: unknown type name ‘my_int’
my_int b = 20;
^

So as you can see, the compiler couldn't recognize the type 'my_int' in the 'my_func' function, as the scope under which typedef was valid was the scope of the 'main' function.

Then there are some other differences between a typedef and a #define. Like, using a #define, you can also create aliases for constant values - I mean, you can do something like '#define PIE 3.14'. Also, as you'd have observed, a #define is not terminated by a semicolon while a typedef is. 

Now, one would still ask why we need 'typedef'. Or in other words, where does a typedef comes in real handy? Well, suppose there's a piece of code with functions that return 'pointer to a function which accepts two integers as arguments as returns a character value'.

In case you aren't aware, such a pointer is declared in the following way:

char (*fn_ptr) (int, int);

Where fn_ptr is the name of such a pointer.

So rather than having to use this kind of lengthy and complex declaration again and again as the return type of other functions, you can just make fn_ptr a type using typedef:

typedef char (*fn_ptr) (int, int);

And now you can just use 'fn_ptr', and it will signify a pointer of this type. 

Another use of typedef involves using in code a user-defined data type which corresponds to the native type provided by the underlying platform on which the code runs. This way, typedefs help make easy to understand and maintain any piece of code that's developed to run on multiple platforms. 

Conclusion

So in this tutorial, we discussed the basics of typedefs, including what typedef does, how it is different from a #define directive, and how typedefs can save your day by making code easy to read, understand, as well as maintain.

You're advised to try out examples we used here as well as develop more examples to get a better understanding of typedefs, and as always, in case of any doubt or query, drop in a comment below.

Share this page:

0 Comment(s)