Email: support@essaywriterpros.com
Call Us: US - +1 845 478 5244 | UK - +44 20 7193 7850 | AUS - +61 2 8005 4826

Association for Computing Machinery

Linux C programming tutorial Part 28 – Typedefs

On this page

  1. Typedef in C Programming Language
  2. Conclusion

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; }