Easy Tutorial
❮ C Exercise Example59 C Examples Palindrome Number ❯

C provides the typedef keyword, which you can use to give a type a new name. The following example defines a term BYTE for a single-byte number:

typedef unsigned char BYTE;

After this type definition, the identifier BYTE can serve as an abbreviation for the type unsigned char, for example:

BYTE  b1, b2;

By convention, uppercase letters are used when defining types to remind the user that the type name is a symbolic abbreviation, but you can also use lowercase letters, as follows:

typedef unsigned char byte;

You can also use typedef to give a new name to a user-defined data type. For example, you can use typedef with a structure to define a new data type name, and then use this new data type to directly define structure variables, as shown below:

Example

#include <stdio.h>
#include <string.h>

typedef struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} Book;

int main( )
{
   Book book;

   strcpy( book.title, "C Tutorial");
   strcpy( book.author, "tutorialpro"); 
   strcpy( book.subject, "Programming Language");
   book.book_id = 12345;

   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book ID : %d\n", book.book_id);

   return 0;
}

When the above code is compiled and executed, it produces the following result:

Book title : C Tutorial
Book author : tutorialpro
Book subject : Programming Language
Book ID : 12345

typedef vs #define

#define is a C directive used to define aliases for various data types, similar to typedef, but they have the following differences:

Here is the simplest usage of #define:

Example

#include <stdio.h>

#define TRUE  1
#define FALSE 0

int main( )
{
   printf( "Value of TRUE: %d\n", TRUE);
   printf( "Value of FALSE: %d\n", FALSE);

   return 0;
}

When the above code is compiled and executed, it produces the following result:

Value of TRUE: 1
Value of FALSE: 0
❮ C Exercise Example59 C Examples Palindrome Number ❯