- How to define a
structwith mixed member types —char[],int, andfloat[] - Why the semicolon after the closing brace of a struct definition is mandatory
- How to access members with the dot operator (
.) - Why strings must be assigned with
strcpy, not= - How a structure behaves like a single variable that happens to contain multiple fields
Why Group Related Data?
A student is not one thing — it's several things: a name, a roll number, and marks in multiple subjects. Before structures, you had two options. One, declare separate variables: char name[50];, int roll;, float marks1, marks2, marks3;. This works for a single student, but the moment you have ten students, you're juggling 60 variables — one slip and you're printing Alice's marks next to Bob's name. Two, use parallel arrays: name[50][10], roll[10], marks[3][10]. This scales but is fragile — nothing enforces that index i of each array refers to the same student, and adding a new field means adding a new parallel array.
A structure solves both problems. You define one blueprint — "a student has a name, a roll number, and three marks" — and then struct student s1; declares a single variable that contains all of that. Passing a student to a function means passing one argument, not five. Copying a student means one assignment, not five. Iterating over students means an array of structures, where each element is a complete record.
This first assignment is deliberately simple: just define the structure and use it. The goal isn't the complexity of the program — it's internalizing the pattern. Once you're comfortable with struct student { ... };, everything else in this series (books, points, dates, employees) is the same pattern with different fields. And the larger idea — that you can invent your own types, not just use int and float — is one of the most powerful concepts in C.
💡 In one line: struct student { ... }; defines a new type; struct student s; creates one student; s.roll = 101; sets a field.
The Structure Blueprint
A struct definition is a blueprint, not a variable. It tells the compiler "whenever I ask for a struct student, reserve enough memory for a 50-character name, an integer, and three floats." No memory is allocated until you declare a variable of the type.
struct student {
char name[50];
int roll;
float marks[3];
}; /* <-- the semicolon is REQUIRED */
Three things to notice. First, char name[50] holds up to 49 characters plus a null terminator — enough for most names. Second, float marks[3] is an array inside a struct, which is legal and common; it holds marks for three subjects. Third, the semicolon after the closing brace is mandatory — forgetting it is one of the most common beginner errors, and the compiler's error message ("expected ';' after struct definition") is usually far from the actual mistake.
| Member | Type | Purpose | How to assign |
|---|---|---|---|
name | char[50] | Student's full name | strcpy(s.name, "Alice") |
roll | int | Roll number | s.roll = 101 |
marks[0] | float | Marks in subject 1 | s.marks[0] = 85.5 |
marks[1] | float | Marks in subject 2 | s.marks[1] = 90.0 |
marks[2] | float | Marks in subject 3 | s.marks[2] = 78.5 |
Complete C Program
Program: define a student structure and print one record
#include <stdio.h>
#include <string.h>
struct student {
char name[50];
int roll;
float marks[3];
};
int main(void) {
struct student s;
/* Assign values to the members */
strcpy(s.name, "Alice Johnson");
s.roll = 101;
s.marks[0] = 85.5;
s.marks[1] = 90.0;
s.marks[2] = 78.5;
/* Print the record */
printf("Student Record\n");
printf("--------------\n");
printf("Name : %s\n", s.name);
printf("Roll : %d\n", s.roll);
printf("Marks : %.1f, %.1f, %.1f\n",
s.marks[0], s.marks[1], s.marks[2]);
return 0;
}
And here's a variant that reads the record from the user with scanf, which is how you'd use it in a real program:
Variant: read student data from the user
#include <stdio.h>
#include <string.h>
struct student {
char name[50];
int roll;
float marks[3];
};
int main(void) {
struct student s;
printf("Enter name: ");
scanf(" %49[^\n]", s.name); /* read up to newline, max 49 chars */
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks in three subjects: ");
scanf("%f %f %f", &s.marks[0], &s.marks[1], &s.marks[2]);
printf("\nStudent Record\n");
printf("--------------\n");
printf("Name : %s\n", s.name);
printf("Roll : %d\n", s.roll);
printf("Marks : %.1f, %.1f, %.1f\n",
s.marks[0], s.marks[1], s.marks[2]);
return 0;
}
The first version is the assignment answer — it demonstrates the struct, the members, and how to print them. The second version is what you'd actually write in a real program: it reads the record from the user. The " %49[^\n]" format is a scanf idiom for reading a whole line of text (including spaces) into a char array, stopping at the newline, with a 49-character cap to prevent overflow. It's more verbose than %s (which stops at the first space) but essential for names like "Alice Johnson".
Program Output
Student Record
--------------
Name : Alice Johnson
Roll : 101
Marks : 85.5, 90.0, 78.5
And for the read-from-user variant:
Enter name: Alice Johnson
Enter roll number: 101
Enter marks in three subjects: 85.5 90.0 78.5
Student Record
--------------
Name : Alice Johnson
Roll : 101
Marks : 85.5, 90.0, 78.5
Try changing the values and re-running — the output should change to match. Try a name with two words to see the difference between %s (which reads only the first word) and the %[^\n] idiom (which reads the whole line).
Step-by-Step Explanation
Let's walk through the program piece by piece.
1. The struct definition (outside main)
struct student { ... }; sits outside main, which means every function in the file can use the type. If you put it inside main, it's local to that function — fine here, but bad practice once your program has multiple functions. The four members are: name (a 50-char array), roll (an int), and marks (a 3-float array). Notice the semicolon after the closing brace — it's required because the whole thing is a single declaration statement.
2. Declaring a variable
struct student s; declares a variable named s of type struct student. On a typical system, s occupies about 50 + 4 + 12 = 66 bytes (plus a few bytes of padding — the exact size is revealed by sizeof(struct student)). Unlike a definition, this declaration actually allocates memory, and s is a single variable holding all three fields.
3. Assigning members
The dot operator (.) accesses a member of a struct variable: s.name, s.roll, s.marks[0]. For name, we can't write s.name = "Alice Johnson" — in C, an array name is not assignable. Instead, strcpy(s.name, "Alice Johnson") copies the string byte by byte into the array, including the null terminator. For roll, a direct assignment works: s.roll = 101;. For marks, we assign each element individually: s.marks[0] = 85.5; and so on.
4. Printing the record
Each printf pulls out one member: %s for the name string, %d for the roll number, %.1f for marks with one decimal place. The formatting isn't required by the assignment, but it's what separates "works" from "works well" — readable output is a small but real part of good programming.
5. Why strcpy needs <string.h>
The strcpy function is declared in the standard header <string.h>. Without the include, the compiler assumes an implicit declaration and issues a warning (or an error in C99 and later, depending on flags). This is why the second line of the program is #include <string.h> — it pulls in the prototype for strcpy.
6. Memory layout (why sizeof isn't just the sum)
If you print sizeof(struct student) on a typical 64-bit system, you'll see 68, not 66. The extra 2 bytes are padding inserted by the compiler for alignment — the int member needs to be at a 4-byte-aligned address, so the compiler adds filler bytes between name (49 chars) and roll. This padding is invisible to your code but matters for memory layout, file I/O, and network protocols. Use sizeof to know the true size, and never assume it equals the sum of members.
7. Why the struct is defined outside main
Although defining it inside main works for this simple program, defining it at file scope (outside any function) is the idiomatic C style. It makes the type available to every function in the file — which is essential once you have printStudent(), readStudent(), and so on. The pattern scales, and that's the point: this assignment is the seed of the pattern you'll use for all 25 structure assignments.
🎯 Remember this: struct student { ... }; is a type definition (semicolon required). struct student s; is a variable declaration. Access members with the dot operator. Use strcpy for string members — = doesn't work for arrays. And the size of a struct is at least the sum of its members, often more due to padding.
Frequently Asked Questions
1. Why does my compiler give "expected ';' after struct definition"?
You forgot the semicolon after the closing brace of the struct definition. struct student { ... } without ; is incomplete — the semicolon is part of the declaration statement. This is one of the most common beginner mistakes in C, and the compiler error message is notoriously unhelpful about pinpointing the actual spot.
2. Why can't I write s.name = "Alice"?
Because name is a char array, and array names in C are not assignable — they decay to pointers, but the pointer itself is fixed (you can't reassign the array). To copy a string into an array, use strcpy(s.name, "Alice"), which copies each character (and the null terminator) into the array. Alternatively, you can initialize at declaration time: struct student s = {"Alice", 101, {85.5, 90.0, 78.5}}; — but that's initialization, not assignment, and only works at the point of declaration.
3. Can I have an array inside a struct?
Yes. float marks[3] is a perfectly valid member — it stores three floats inside the same struct. Accessing an element uses two operators: s.marks[0] is "the first element of the marks array inside s." This pattern is common for fixed-size arrays like subject marks, coordinates, or RGB values. Dynamic arrays inside structs are possible too, but require a pointer + malloc, which is a larger topic.
4. Why does sizeof(struct student) return more than the sum of members?
Because of padding. The compiler inserts filler bytes between members so each member is aligned on a boundary the CPU can access efficiently (typically 4 or 8 bytes). On a 64-bit system, char name[50] (50 bytes) + int roll (4 bytes) + float marks[3] (12 bytes) = 66 bytes, but the actual sizeof is often 68 because of 2 bytes of padding after roll. This padding is why sizeof is not equal to the sum, and why you should never assume the two are the same.
5. Can I declare multiple students in one line?
Yes: struct student s1, s2, s3; declares three separate student variables. Each has its own copy of name, roll, and marks. You can also declare an array: struct student class[30]; for 30 students, which is the subject of assignment 25 in this series. For now, one variable is enough to demonstrate the pattern.
6. What's the difference between struct student s; and typedef struct { ... } student;?
The first is the traditional C style: you must always write struct student to refer to the type. The second uses typedef to create an alias so you can write student s; without the struct keyword. Both are valid; the typedef version is shorter but requires understanding the typedef syntax. Many modern C codebases use typedef for cleaner code, and we'll use it in later assignments. For this one, the plain struct student is fine — and it's what most textbooks and interviews expect.
💡 Tip: After this program, try adding a fourth member — say, char gender; — and set it to 'F' or 'M'. Then try adding a fifth — char address[100] — and see how the struct grows. Once you're comfortable with this, assignment 2 (initialize and display) and assignment 3 (average marks) will be easy — they're the same pattern with more computation.