The one-line employee
In the last assignment, we filled the employee struct one field at a time: three lines of code, one for the name, one for the ID, one for the salary. It works, but it's a bit long. What if you could fill all three fields in one line? What if that one line also made it obvious which value belongs to which field?
You can. C lets you fill a struct at the same moment you declare it, by putting the values in curly braces right after the variable name. And if you want extra clarity, you can even name each field so the order doesn't matter. This assignment is about that shortcut, and about the three different ways to use it. By the end, you'll know exactly which one to reach for, depending on where the data comes from.
Three ways to fill the record
There are three common ways to fill an employee struct, and each one fits a different situation. The first is the one-line brace form: struct employee e = {"Alice Johnson", 1001, 55000.0f};. The three values fill the three fields in the order they're declared — name, then ID, then salary. It's short, and it's the style most C programmers use for known values.
The second is the named form: {.name = "Alice Johnson", .id = 1001, .salary = 55000.0f}. It's a few extra characters, but each field name makes the mapping explicit. If you ever reorder the struct, the named form keeps working while the brace form would silently shuffle the values into the wrong fields. For employee records with three different types, the named form is arguably clearer, because you don't have to remember that "name comes first, then ID, then salary".
The third is the user-input form: scanf each field from the user. This is what you'd use in a real program, because the employee data comes from somewhere — a form, a database, a keyboard — and isn't known at compile time. The struct is declared empty, then filled field by field as the user types the values.
💡 In one line: {"Alice", 1001, 55000.0f} fills in order, {.name = "Alice", .id = 1001, .salary = 55000.0f} fills by name, and scanf fills from the user. Pick the one that matches where the data comes from.
The fill patterns
Here are the three styles side by side, so you can compare them.
/* Fill in order */
struct employee a = {"Alice Johnson", 1001, 55000.0f};
/* Fill by name (C99) */
struct employee b = {
.name = "Alice Johnson",
.id = 1001,
.salary = 55000.0f
};
/* Fill from the user */
struct employee c;
scanf(" %49[^\n]", c.name);
scanf("%d", &c.id);
scanf("%f", &c.salary);
All three produce the same employee record. The choice is about clarity and about where the data comes from. If the data is known at compile time and the struct is short, the brace form is fine. If the struct has many fields or might change later, use the named form. If the data comes from outside the program, use scanf.
| Style | When to use | Why |
|---|---|---|
| In order | Small struct, fixed values | Shortest, easiest to type |
| By name | Larger struct, or code that will be edited later | Self-documenting, robust to reordering |
| From the user | Data comes from outside the program | The only option — values aren't known at compile time |
Complete C Program
Program: initialize an employee and print the record
#include <stdio.h>
struct employee {
char name[50];
int id;
float salary;
};
int main(void) {
struct employee e = {"Alice Johnson", 1001, 55000.0f};
printf("Employee Record\n");
printf("---------------\n");
printf("Name : %s\n", e.name);
printf("ID : %d\n", e.id);
printf("Salary : %.2f\n", e.salary);
return 0;
}
And here are the other two styles — first with named fields, then reading from the user:
Variant 1: named fields
#include <stdio.h>
struct employee {
char name[50];
int id;
float salary;
};
int main(void) {
struct employee e = {
.name = "Alice Johnson",
.id = 1001,
.salary = 55000.0f
};
printf("Employee Record\n");
printf("---------------\n");
printf("Name : %s\n", e.name);
printf("ID : %d\n", e.id);
printf("Salary : %.2f\n", e.salary);
return 0;
}
Variant 2: read from the user
#include <stdio.h>
struct employee {
char name[50];
int id;
float salary;
};
int main(void) {
struct employee e;
printf("Enter name: ");
scanf(" %49[^\n]", e.name);
printf("Enter ID: ");
scanf("%d", &e.id);
printf("Enter salary: ");
scanf("%f", &e.salary);
printf("\nEmployee Record\n");
printf("---------------\n");
printf("Name : %s\n", e.name);
printf("ID : %d\n", e.id);
printf("Salary : %.2f\n", e.salary);
return 0;
}
All three print the same record. The only differences are how the values got in and how explicit the code is. For the assignment, any of them is a fine answer; for a real program, use whichever fits the data source.
Program Output
Employee Record
---------------
Name : Alice Johnson
ID : 1001
Salary : 55000.00
And for the user-input version:
Enter name: Bob Smith
Enter ID: 2042
Enter salary: 47500.75
Employee Record
---------------
Name : Bob Smith
ID : 2042
Salary : 47500.75
Try a name with two or three words, like "Mary Jane Watson". The %49[^\n] in the scanf reads the whole line, so the full name comes through. A plain %s would have stopped at the first space and left "Jane Watson" to confuse the next scanf.
Step-by-Step Explanation
Short program, quick walk-through of the main version.
1. The struct
Same struct employee as the last assignment. Three fields of three different types. Nothing new here.
2. The one-line fill
struct employee e = {"Alice Johnson", 1001, 55000.0f}; fills all three fields in one statement. The compiler sees a string, an int, and a float, and knows where each one goes based on the field order. This works only at the point of declaration — once the variable exists, you have to fill it field by field (or use a compound literal, which is a more advanced trick).
3. The named form
The named form uses the .field = value syntax from C99. The compiler matches each value to its field by name, so the order stops mattering. This is safer if the struct changes later — if someone reorders the fields, the named form still fills the right fields, while the in-order form would silently swap the values. For a three-field struct, the extra typing is minor; for a ten-field struct, it's essential.
4. The scanf version
For user input, the struct is declared empty and each field is filled by a scanf. The name uses " %49[^\n]" to read a whole line with spaces, up to 49 characters. The ID uses "%d" with &e.id to store the number in the field's address. The salary uses "%f" with &e.salary. Each scanf call targets a different field — that's the direct equivalent of the fill-in-order form, but happening at runtime instead of at declaration.
5. Printing the same way
All three variants use identical printf code. That's deliberate — once the struct is filled, the printing doesn't care how it got there. This is one of the nice properties of structs: the code that uses a struct doesn't need to know where the struct came from. It just reads the fields.
6. What happens if you forget a value in the initializer list
If you write struct employee e = {"Alice Johnson", 1001}; (leaving out the salary), the compiler zero-fills the missing field. So e.salary becomes 0.0. This is useful as a default, but it can be a subtle bug if you meant to fill it and forgot. The named form makes this less likely, because leaving out a field is visible in the code.
7. Why this matters for the next assignment
Giving an employee a salary hike (assignment 21) is basically this code plus one line: e.salary = e.salary * 1.10f;. The fill and print parts don't change. So getting comfortable with the fill patterns now means the next assignment is mostly about the salary arithmetic, not about how to structure the program.
🎯 Remember this: use {"Alice", 1001, 55000.0f} for known values, {.name = ..., .id = ..., .salary = ...} for clarity, and scanf for user input. The print code is the same in all three. That's the flexibility of structs — how you fill them doesn't affect how you use them.
Frequently Asked Questions
1. Can I fill the struct after declaring it, but without scanf?
Yes: struct employee e; strcpy(e.name, "Alice"); e.id = 1001; e.salary = 55000.0f; works. That's just the three-assignment style from the last assignment. The brace form only works at declaration, but you can fill each field individually any time after declaring. This is what the scanf version does, except the values come from the user instead of literals.
2. What if I forget a field in the initializer list?
The remaining fields are zero-initialized. So {"Alice", 1001} gives salary = 0.0. This is a feature, not a bug — sometimes you want a partial record and will fill the rest later. But it can also be an oversight, so use the named form for larger structs (or add a comment) to make missing fields obvious.
3. Why do I need the f after 55000.0?
Because without it, 55000.0 is a double, not a float. Assigning a double to a float field works, but the compiler may warn about implicit narrowing. Adding the f makes it a float literal, matching the field type. It's a small habit that keeps your code warning-free with strict compilers.
4. Why does the scanf for name use " %49[^\n]" and not just "%s"?
Because "%s" stops at the first space, so "Alice Johnson" becomes just "Alice", and "Johnson" would be left in the input buffer, breaking the next scanf. The " %49[^\n]" format reads a whole line of text (including spaces) up to 49 characters, stopping at the newline. The leading space skips any leftover whitespace. This is the standard way to read names with spaces.
5. What if the user enters a name longer than 49 characters?
The 49 in the format limits the read to 49 characters — the extra characters stay in the input buffer and are ignored (or cause confusion for the next scanf). The name field has room for 49 visible characters plus a null terminator, so this is the maximum it can hold. Longer names would need a bigger array in the struct.
6. Can I use the same pattern for an array of employees?
Yes — struct employee staff[3] = { {"Alice", 1001, 55000.0f}, {"Bob", 1002, 47500.0f}, {"Carol", 1003, 62000.0f} }; fills three employees in one line. Each inner brace is one employee. This is the same pattern as the book collection from assignment 6 — array of structs, filled with nested braces. The printing is then a loop with staff[i].name, staff[i].id, staff[i].salary.
💡 Tip: After this, try making three employees with different salaries and printing them side by side. Then try computing the average salary. Both are small extensions of the fill-and-print pattern. Once you're comfortable with these, the salary hike assignment (21) is a single line of arithmetic.