Three types, one record
Here's something you couldn't do with the structs we've seen so far. An employee record has three fields of three different types: a name (a string, so a char array), an employee ID (an integer), and a salary (a floating-point number, because salaries have cents). You can't put those three values in an array — arrays hold values of one type. You can't pass them around as three separate variables without losing track of which is which after a while. But a struct? A struct handles this without any trouble at all.
That's the power of a struct in a sentence: it lets you group values of different types under one name. The employee struct is a good example because each field is a different type. When you print the record, you use a different format specifier for each one. When you pass the record to a function, you pass one argument instead of three. When you have a list of employees, one array of structs holds everything — names, IDs, and salaries — without any parallel arrays.
Why a struct, not three variables
For one employee, three separate variables would work fine: char name[50];, int id;, float salary;. The moment you have two employees, you have six variables. Ten employees — thirty variables. And any function that deals with an employee has to take three arguments, and any operation that copies or compares an employee has to do it field by field.
A struct wraps those three fields into a single variable. struct employee e; is one variable holding all three parts of one employee record. Passing an employee to a function is one argument. Copying an employee is one assignment. Having ten employees is an array of structs, where each element is a complete record. This is exactly the kind of grouping structs are designed for, and it's why they appear in every real C codebase.
The types matter here too. The name is a string, so it needs a char array with a fixed size. The employee ID is a number, so int is the natural choice. The salary is money, so it needs decimals — but here's the tricky part: floats have precision issues, and for real financial code you'd store the salary in cents as an integer. For a teaching example, though, a float or double is fine and much simpler to write. We'll talk about the tradeoff in the FAQ.
💡 In one line: struct employee { char name[50]; int id; float salary; }; is the type. struct employee e = {"Alice", 1001, 55000.0f}; is one employee. Print with %s, %d, and %.2f.
The struct and its fields
Three fields, three types:
struct employee {
char name[50];
int id;
float salary;
};
Same as every other struct — don't forget the semicolon after the closing brace. The order matters for the initializer list (name first, then id, then salary), but not for anything else.
| Member | Type | Meaning | How to assign |
|---|---|---|---|
name | char[50] | Employee's full name | strcpy(e.name, "Alice") |
id | int | Employee ID number | e.id = 1001 |
salary | float | Annual salary | e.salary = 55000.0f |
The salary could be an annual amount, a monthly amount, an hourly rate — the struct doesn't care. Whatever number you put in e.salary is what gets printed. The name field is capped at 49 characters plus the null terminator; if the name is longer, it gets truncated. If you need longer names, make the array bigger — but 50 is fine for almost everyone.
Complete C Program
Program: define an employee and print the record
#include <stdio.h>
#include <string.h>
struct employee {
char name[50];
int id;
float salary;
};
int main(void) {
struct employee e;
strcpy(e.name, "Alice Johnson");
e.id = 1001;
e.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;
}
And here's a version that fills the struct with an initializer list — one line and done — plus a variant that reads from the user:
Variant 1: initializer list
#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 : %s (ID: %d), Salary: %.2f\n",
e.name, e.id, 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;
}
The first version is the assignment answer — define, fill, print. The second uses an initializer list for brevity. The third reads from the user. All three produce the same record, and the printing part is the same in all of them. The " %49[^\n]" is the scanf idiom for reading a whole name with spaces, up to 49 characters, stopping at the newline — much better than plain %s, which would stop at the first space.
Program Output
Employee Record
---------------
Name : Alice Johnson
ID : 1001
Salary : 55000.00
And for the user-input version, try your own values:
Enter name: Bob Smith
Enter ID: 2042
Enter salary: 47500.75
Employee Record
---------------
Name : Bob Smith
ID : 2042
Salary : 47500.75
Notice how the salary prints with two decimal places, regardless of whether the input had them. 47500.75 prints as 47500.75; 50000 prints as 50000.00. That's the %.2f doing its job — it gives every salary the same visual format, which is what you want when printing a list of records.
Step-by-Step Explanation
Short program, quick walk-through.
1. The struct definition
struct employee { char name[50]; int id; float salary; }; — three fields of three different types. The name is a 50-character array (so it can hold up to 49 visible characters plus a null terminator). The ID is an int. The salary is a float. The names are for humans reading the code; C doesn't care what you call them as long as you use them consistently.
2. Declaring and filling
struct employee e; creates the variable. Then strcpy(e.name, "Alice Johnson") copies the string into the name field — the strcpy is required because strings in C aren't assignable with =. e.id = 1001 and e.salary = 55000.0f are direct assignments, since those fields are just numbers.
3. Why the initializer list version is shorter
The brace form struct employee e = {"Alice Johnson", 1001, 55000.0f}; does all three assignments in one line, at the point of declaration. It works because the compiler knows the types of the three fields (string, int, float) and can fill them from the values in order. This is the style you'd use when the values are known at compile time; for values that come from the user, you need the scanf version.
4. The scanf for names with spaces
Plain %s in scanf stops at the first space, so "Alice Johnson" would become just "Alice", and "Johnson" would be left in the input buffer to confuse the next scanf. The format " %49[^\n]" fixes this: the leading space skips any whitespace, the 49 limits the read to 49 characters, and [^\n] reads everything up to (but not including) the newline. That's how you read a full name with spaces.
5. Printing with different format specifiers
Three printf arguments, three different format specifiers: %s for the string, %d for the integer, %.2f for the float with two decimal places. This is where structs shine — you reach in, pull out one field at a time, and print each with the format that fits its type. The compiler doesn't check that you're using the right specifier; you have to get that right yourself.
6. The padding and alignment
Inside the struct, the compiler adds padding bytes between the fields so each one lands on an address that the CPU can access efficiently. A 50-char array followed by an int might need 2 bytes of padding between them (so the int starts on a 4-byte boundary). Then another 2 bytes of padding after the int, so the float also starts on a 4-byte boundary. The total sizeof(struct employee) is 60 bytes on most systems, not 58. Never assume the size is the sum of the field sizes; use sizeof.
7. Why the salary is a float, and what to worry about
Floats have about 7 significant decimal digits of precision. That's plenty for salaries up to 99,999.99 with cents, but for larger amounts, you can start to lose precision — a salary of 1234567.89 might come out as 1234567.88 after a calculation. In financial software, this is unacceptable, so real systems store money as integer cents. For a teaching example, a float is fine; but the habit of "for real money, use integers or a decimal type" is worth knowing from the start.
🎯 Remember this: a struct groups fields of different types under one name. Use strcpy for the name string; direct assignment for ID and salary. Print with %s, %d, %.2f. For real financial code, use integer cents instead of floats.
Frequently Asked Questions
1. Why is the name a char array and not a char*?
A char* (pointer) would also work — you'd need to allocate memory for it and remember to free it when done. A fixed array is simpler for teaching: it's inline in the struct, no allocation needed, and no risk of forgetting to free. The tradeoff is that the array has a fixed maximum length, so long names get truncated. For most real-world names, 50 characters is plenty. If you need dynamic names, use a pointer and malloc.
2. Why do salaries need a float and not an int?
Because salaries have cents. If you used an int, 55000.50 would become 55000 — you'd lose the 50 cents. Using a float keeps the fractional part. But as noted, floats have precision issues for very large values. The professional solution is to store cents as an integer — 5500050 instead of 55000.50 — and print it divided by 100. That keeps precision exact. For a beginner program, float is fine.
3. What happens if the name is longer than 49 characters?
It gets truncated — only the first 49 characters fit (plus the null terminator at position 50). The rest is silently dropped. This is why fixed-size arrays have a limit; for names, IDs, and most identifiers, 50 characters is more than enough. For full names with middle names, titles, and suffixes, you might want 100. Or use dynamic allocation (pointer + malloc) to be truly unlimited.
4. Why does my program crash when I use e.name = "Alice"?
Because e.name is a char array, and you can't assign a string literal to an array directly. You have to use strcpy(e.name, "Alice"). Or, if it's at declaration time, use the initializer list: struct employee e = {"Alice", 1001, 55000.0f};. The latter is safe; the former gives a compile error, not a crash, but it's a common beginner mistake.
5. Can I have an array of employees?
Yes — struct employee staff[100]; gives you 100 employee records. Each one has its own name, ID, and salary. You'd fill them in a loop and print them in a loop. This is the foundation for assignment 25 (sorting an array of structures). The struct doesn't change; only where it lives (in an array, indexed by i).
6. How would I give an employee a raise?
Simple: e.salary = e.salary * 1.10f; for a 10% raise. Or for a fixed amount: e.salary += 5000.0f;. The point is that once the data is in the struct, arithmetic works the same as with plain variables. This is the subject of assignment 21 — giving a salary hike. The whole program is basically one line of arithmetic plus the same print code you've seen here.
💡 Tip: After this, try adding a second field to the struct — say, char department[30] for the employee's department. Then update the print code to show it. You'll see that adding a field is a small, local change — this is one of the nice properties of structs. Then try adding a "years of service" field as an int and print that too. The struct grows; the print pattern stays the same.