- 2071 Chaitra · 1+1+2 marks
Structure tag
Short answer
A tag is the optional name after struct that names the type. It is not mandatory in the definition if variables are declared simultaneously (struct { int x; } p;). In separate variable declarations, the tag must be included (struct student s1;). If typedef is used, the tag can be omitted.
What is a tag? Must a tag be included in a structure type definition? Must a tag be included in a structure variable declaration? Explain.
What is a tag?
A tag is the optional name written after the keyword struct in a structure definition. It names the structure type, so that the type can be used again later.
struct student { /* "student" is the tag */
char name[20];
int roll;
};
Must a tag be included in the structure definition?
No. A structure can be defined without a tag, but then variables must be declared at the same time (in the definition), and the type cannot be used again later because it has no name.
struct { int x, y; } p; /* no tag, variable p declared here */
Must a tag be included in a variable declaration?
Yes, when the variable is declared separately from the definition, the tag is needed along with struct, as in struct student s1;. If the variable is declared together with the definition (struct {...} p;) no tag is needed. With typedef, the new type name can replace struct tag.
#include <stdio.h>
struct book { /* "book" is the tag */
char title[30];
float price;
} b1; /* variable declared with the definition */
struct book b2; /* variable declared later using the tag */
struct { /* no tag: variable must be declared here */
int x, y;
} p;
int main(void)
{
b1.price = 250;
b2.price = 300;
p.x = 1;
printf("%.0f %.0f %d\n", b1.price, b2.price, p.x);
return 0;
}
Output: 250 300 1
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Variable declaration versus definition · Computer Programming, chapter 3
- Declaration vs definition; C program structure · Computer Programming, chapter 3
- Org structure definition; line organization pros/cons · Organization and Management, chapter 5