Typedef in c
Typedef is a keyword that is used to give an alternative name to the already existing data type in a c program. It is usually used in user-defined data types.
Syntax: typedef<existing_datatype_name><alternative_name>
Eg. typedef long int alt;
alt a=10;
In the above example, we have given an alternative name “alt” for “long int”.
If we want to declare a long int variable, we can simply write alt b; instead of long int b;
demo program
1 2 3 4 5 6 7 8 9 10 11 12 | #include<stdio.h> int main() { typedef long int alt; alt a=1000,b=2000; typedef unsigned int un; un p=-10,q=20; printf("typedef in c\na=%ld\n b=%ld\n p=%d\n q=%d",a,b, p,q); return 0; } |
output:
typedef with structure
We can use the typedef to give an alternative name to the complicated structure datatype.
Usually, we define a structure as,
struct structure_datatype
{
datatype member 1;
datatype member 2;
}
And to declare the variable of the structure we declare as,
struct structure_datatype variable_name;
As you can see, we have to write such a big statement for the declaration of the structure variable.
can we declare the alternative name for the statement “struct structure_datatype”?
Yes, we can do so, and this is possible with the help of syntax “typedef”.
We just have to write “typedef before the definition of the structure and, after defining the structure, we have to declare the meaningful name for the structure data type.Now let's see the syntax.
typedef struct structure_datatype
{
datatype member 1;
datatype member 2;
}alternative name for the structure datatype;
Lets, take an example,
typedef struct student
{
int roll_no;
char name;
}stud;
now while declaring the variable for the student data_type,
instead of writing “ struct student s1” we can simply write “ stud s1”.
Here “ s1 “ is the variable name.
We can declare as many variable names as we want to declare.
below are two examples on typedef
1. 1. Typedef with Structure.
a. Without dynamic memory allocation.
b. With dynamic memory allocation using a pointer structure variable.
2. 2. Typedef with array of structure
a. typedef with the structure of the array.
b. b. typedef with the structure of the array using the pointer structure variable.
1. typedef with structure
a. Without dynamic memory allocation.
#include<stdio.h> typedef struct book { char name[20]; int prize; }bb; int main() { bb t; printf("Enter the name of the book and its prize:\n"); scanf("%s %d",t.name,&t.prize); printf("\n name= %s\n prize=%d",t.name,t.prize); return 0; } |
b.
With
dynamic memory allocation using the pointer structure variable.
#include<stdio.h> #include<stdlib.h> typedef struct book { char name[20]; int prize; }bb; void main() { bb *t; t=(bb*)malloc(sizeof(bb)); printf("Enter the name of the book and its prize:\n"); scanf("%s %d",t->name,&t->prize); printf("\n name= %s\n prize=%d",t->name,t->prize); return 0; } |
In the above program a and b, “bb” is the alternative name of “struct book”, which is possible using typedef.
The difference between a. and b,
The structure variable in the program "a" is a normal variable, not a pointer variable, and program "b" contains a pointer variable whose memory is allocated dynamically.
output:
2. Typedef with an array of structures.
a. typedef with the structure of the array.
include<stdio.h> typedef struct stationary { char name[10]; int quantity; int prize; }stat; int main() { stat s[5]; int i; printf("Enter the names of stationary materials,quantity and prize:\n\n"); for(i=0;i<5;i++) scanf("%s %d %d",s[i].name,&s[i].quantity,&s[i]prize); printf("\nname quantity prize\n"); for(i=0;i<5;i++) printf("%s %d %d\n",s[i].name,s[i].quantity,s[i]prize); return 0; } |
b. typedef with the structure of the array using the pointer structure variable.
#include<stdio.h> typedef struct stationary { char name[10]; int quantity; int prize; }stat; int main() { stat s[5]; stat *p=NULL; int i; p=s; printf("Enter the names of stationary materials,quantity and prize:\n\n"); for(i=0;i<5;i++) { scanf("%s %d %d",p->name,&p->quantity,&p->prize); p++; } p=s; printf("\nname quantity prize\n"); for(i=0;i<5;i++) { printf("%s %d %d\n",p->name,p->quantity,p->prize); p++; } return 0; } |
In the above program a. and b. “stat” is the alternative name of “struct stationery”.
The difference between program a. and b. is,
- The program a. contains a normal array of structure variables which is s[5].
- Program b. contains a normal array of structure s[5] and pointer *p.
The base address of s[5] is initialized to "p" . ie. p=s.
The input in s[5] is taken using pointer *p. all the operations in s[5] are performed using *p without touching s[5].
To increment the value in p which is the base address of s[5], p is incremented as p++ will increment from s[0] tos[1] and so on till s[4].
After taking the input we have to print the input.
But, now p contains the address of position s[4].
To come back again to s[0] p=s; is used again.
Again the same process of incrementation(p++) is used to move from m[0] to m[1] till m[4] for printing the data under m[5].
Output:
Post a Comment
Post a Comment