C TYPEDEF | TYPEDEF IN C PROGRAMMING | C TYPEDEF WITH EXAMPLE | TYPEDEF VS # DEFINE | MY CS TUTORIAL
typedef is a keyword used in C language to assign alternative names to
existing types.
This keyword, typedef typically employed in association with user-defined data types in cases if the names of datatypes turn out to be a little complicated or intricate for a programmer to get or to use within programs.
Its mostly used with user defined data types, when names of data types
get slightly complicated.
Following is the general syntax for using typedef,
typedef existing_name alias_name
typedef can be used to give a name to user defined data type as
well.
Let’s see its use with structures.
typedef struct
{
type member1;
type member2;
type member3;
} type_name ;
Here type_name represents the stucture definition associated with it. Now
this type_name can be used to declare a variable of this stucture type.
type_name t1, t2 ;
By convention, uppercase letters are used for these definitions to remind the
user that the type name is really a symbolic abbreviation, but you can use
lowercase, as follows:
typedef unsigned char byte;
You can use typedef to give a name to your user-defined data types as well. For example, you can use typedef with structure to define a new data type and then use that data type to define structure variables directly as follows:
#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, "Mr. 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;
}
When the above code is compiled and executed, it produces the following result:
Book title : C Programming
Book author : Mr. Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
#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:-
The following example shows how to use #define in a program:
#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
Typedef in C | Typedef | Learn typedef | c- typedef | typedef in C and C++
Typedef in c by My CS Tutorial |
Typedef:-
typedef is a keyword used in C language to assign alternative names to
existing types.
This keyword, typedef typically employed in association with user-defined data types in cases if the names of datatypes turn out to be a little complicated or intricate for a programmer to get or to use within programs.
Its mostly used with user defined data types, when names of data types
get slightly complicated.
Following is the general syntax for using typedef,
typedef existing_name alias_name
typedef can be used to give a name to user defined data type as
well.
Let’s see its use with structures.
typedef struct
{
type member1;
type member2;
type member3;
} type_name ;
Here type_name represents the stucture definition associated with it. Now
this type_name can be used to declare a variable of this stucture type.
type_name t1, t2 ;
By convention, uppercase letters are used for these definitions to remind the
user that the type name is really a symbolic abbreviation, but you can use
lowercase, as follows:
typedef unsigned char byte;
You can use typedef to give a name to your user-defined data types as well. For example, you can use typedef with structure to define a new data type and then use that data type to define structure variables directly as follows:
#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, "Mr. 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;
}
When the above code is compiled and executed, it produces the following result:
Book title : C Programming
Book author : Mr. Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
typedef vs #define
#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:-
- typedef is limited to giving symbolic names to types only, whereas #define can be used to define alias for values as well, e.g., you can define 1 as ONE, etc.
- typedef interpretation is performed by the compiler whereas #define statements are processed by the preprocessor.
The following example shows how to use #define in a program:
#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
Typedef in C | Typedef | Learn typedef | c- typedef | typedef in C and C++
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment