Monday, April 4, 2016

TypeDef

The C programming language provides a keyword called typedef, which you can use to give a type, a new name. Following is an example to define a term BYTE for one-byte number





Example :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#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 Programming"); 
strcpy( book.author, "Nuha Ali"); 
strcpy( book.subject, "C Programming Tutorial");
 book.book_id = 6495407; 
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); 
return 0; 
}

Typedef vs #define :

 1) #define is a C-directive which is also used to define the aliases for various data types similar to typedef but with the following differences .

2) 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.

3) typedef interpretation is performed by the compiler whereas #definestatements are processed by the pre-processor.

Share this

0 Comment to "TypeDef "

Post a Comment