Typedef In C Language

 

 Typedef In C Language

Creating a new datatype using existing datatype is known as typedef
typedef int BYTE;
Here Byte is new Datatype 
 
#include <stdio.h>
#include <string.h>
#include <conio.h>
 
typedef struct Books {
   char title[150];
   char author[150];
   char subject[200];
   int book_id;
} Book;
 
int main( ) {

   Book book;
 
   strcpy( book.title, "Java Programming");
   strcpy( book.author, "Dennis Ritchie"); 
   strcpy( book.subject, "java Programming Tutorial");
   book.book_id = 1224;
 
   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);

   getch();
}


typedef vs #define #define is a C-directive which is also used to define the aliases names for various datatypes.
It is Similar to typedef but following differences.
typedef is limited to giving symbolic names to types only where as #define can be used to define alias for values as well, q., you can define 1 as ONE etc.
typedef interpretation is performed by the compiler whereas #define statements are processed by the pre-processor.
#include <conio.h>
#include <stdio.h>
 
#define TRUE  1
#define FALSE 0
 
void main( ) {
   printf( "Value of TRUE : %d\n", TRUE);
   printf( "Value of FALSE : %d\n", FALSE);

   getch();
}


No comments:

Post a Comment