16.11 – Typedef in C language – Quiz

by subbu on September 29, 2014

C provides different data types to allocate the memory to store the data belong to different types of different ranges.

Some times we may feel that, declaration of variables is too technical and lengthy. Let us see some of such cases

static unsigned int x;

Nothing wrong with the above declaration but a bit lengthy

struct node
{
 int data;
 struct node *link;
};

struct node *head=(struct node*)malloc(sizeof(struct node));

Here we find repetition of words “struct node” for nearly 3 times in a single statement

We can make these so simple and short using typedef. Using typedef we can set an alias name to the type

Typedef syntax

typedef static unsigned int counter;

Here we have created an alias “counter” to “static unsigned int”. Now we can use “counter” in place of “static unsigned int”

static unsigned int x;

and

counter x;

will have the same effect

even we can create an alias to “struct node” as

typedef struct node nd;

Here we have created an alias “nd” to “struct node”. Now we can use “nd” in place of “struct node”

struct node
{
 int data;
 struct node *link;
};
typedef struct node nd;
nd *head=(nd*)malloc(sizeof(nd));

Here it is demonstrated that, some times typedef makes the job easier

In the above example struct definition and creating alias are written in separate statements but can be written in a single statement as

typedef struct node
{
 int data;
 struct node *link;
}nd;   /* creating alias but not the variable */
nd *head=(nd*)malloc(sizeof(nd));

Note: While defining the self referential struct see that the self reference within the struct must not be declared with the typedef.

typedef struct node
{
 int data;
 nd *link; /* can't be done */
}nd;

nd *head=(nd*)malloc(sizeof(nd));

Even it is also possible to create an alias to the struct type without a name tag

#include<stdio.h>
typedef struct   /* without a name tag */
{
 int x;
 int y;
}nm;   /* creating alias */
int main()
{
 nm a={10,20};
 printf("x=%d\ny=%d\n",a.x,a.y);
 return 0;
}

Output
x=10
y=20

Now we see a couple of examples

Specification : Accept two integers and print the sum of them

#include<stdio.h>
typedef int number;
number getsum(number,number);
number main()
{
 number a,b,c;
 printf("Enter two integers:\n");
 scanf("%d%d",&a,&b);
 c=getsum(a,b);
 printf("Sum %d",c);
 return 0;
}
number getsum(number x,number y)
{
 return x+y;
}

Execution
Enter two integers:
10
20
Sum 30

Example explained:
In the above example created an alias “number” to “int” and used along the program. Here it improves the readability.

Example

#include<stdio.h>
struct address
{
 char name[50];
 char street[50];
 char city[50];
 long int pin;
};
typedef struct address ads;
int main()
{
 ads x={"Black","M.G.Road","Hyd-bad",524201};
 printf("Address:\n");
 printf("Name:%s\nStreet:%s\nCity:%s\nPin code:%ld",x.name,x.street,x.city,x.pin);
 return 0;
}

Output:
Address:
Name:Black
Street:M.G.Road
City:Hyd-bad
Pin code:524201

Example explained:
Here an alias “ads” is created to the type “struct address” and used along the program

Quiz

1. What would be the output of following program?

#include<stdio.h>
typedef struct num
{
 int x;
}a;
int main()
{
 struct num b;
 b.x=45;
 printf("x=%d",b.x);
 return 0;
}
Show Answer

Output
x=45

Though “a” is the alias to “struct num” we can still use “struct num” to instantiate

2. What would be the output of following program?

#include<stdio.h>
typedef int number;
int main()
{
 int x=10;
 number *p;
 p=&x;
 printf("x=%d",*p);
 return 0;
}
Show Answer

Output

x=10

3. What would be the output of following program

#include<stdio.h>
typedef struct
{
 int x;
 int y;
}a;
int main()
{
 a var={10,20};
 printf("x=%d\n",var.x);
 printf("y=%d\n",var.y);
 return 0;
}
Show Answer
Output
x=10
y=20

4. What would be the output of following program?

#include<stdio.h>
typedef struct num
{
 int x;
 int y;
}a={10,20};
int main()
{
 printf("x=%d\n",a.x);
 printf("\ny=%d\n",a.y);
 return 0;
}
Show Answer
Output
Error

Here “a” is an alias name to struct num but, not a variable belongs to struct. Hence we can’t initialize.

5. What would be the output of following program?

#include<stdio.h>
typedef struct num
{
 int x;
 int y;
}a;
int main()
{
 struct num b={10,20};
 a c=b;
 printf("x=%d\n",c.x);
 printf("y=%d\n",c.y);
 return 0;
}
Show Answer
Output
x=10
y=20

Here both “b” and “c” are variables belonging to the same “struct num” type but one is created using “struct num” and another is created using alias.

Hence one struct type variable can be directly assigned to another struct variable

6. What would be the output of following program?

#include<stdio.h>
typedef struct name
{
 char *p;
}nm;
int main()
{
 nm x;
 x.p="hello";
 printf("%s",x.p);
 return 0;
}
Show Answer
Output
hello

Previous post:

Next post: