- 2070 Chaitra · 2 marks
Use of typedef with structures
Short answer
typedef gives a new name to a struct type, so variables can be declared without 'struct' keyword. For example, 'typedef struct { char name[20]; int roll; } Student;' allows 'Student s;' instead of 'struct student s;'. The tag becomes optional.
Explain the use of typedef keyword in structures.
typedef gives a new name to an existing data type. With a structure, it lets us declare variables without writing the keyword struct each time, which shortens the code.
typedef struct student {
char name[20];
int roll;
} Student;
Student s1, s2; /* instead of: struct student s1, s2; */
Here Student is the new type name (not a variable). The tag student is optional; it can be left out, as in the program below.
#include <stdio.h>
typedef struct {
char name[20];
int roll;
} Student; /* Student is now a type name */
int main(void)
{
Student s = {"Ram", 7}; /* no 'struct' keyword needed */
printf("%s %d\n", s.name, s.roll);
return 0;
}
Output: Ram 7
✓ Answer checked by Claude Opus 5.5 (high effort)