- How to fill a rectangle struct at the same line where you declare it
- How to use named fields (
.length = ...) so the order stops mattering - How to read the length and width from the user with
scanf - How to print the dimensions cleanly so they read like real measurements
- Why getting the formatting right makes the next assignment (area and perimeter) easier to read
Filling a rectangle in one line
In the previous assignment, we defined the rectangle struct and filled each field with a separate line. Here we do the same thing, but tighter. struct rectangle r = {12.5f, 7.0f}; is the whole initialization. The first value goes into length, the second into width, because that's the order the fields appear in the struct. That's it — one line and the rectangle is fully ready.
There's also a second style where you name each field: {.length = 12.5f, .width = 7.0f}. It's a bit longer, but you can write the fields in any order. If you ever swap length and width in the struct definition, the named form still fills the right fields. The unnamed form would silently put 12.5 into width and 7.0 into length — which is the kind of bug you don't notice until you're computing areas and getting strange numbers.
Both styles are worth knowing. The short one is fine for a two-field struct like this. The named one is what you want in real code, especially once your structs grow beyond three or four fields. We'll show both here, plus a version that reads the sides from the user.
💡 In one line: struct rectangle r = {12.5f, 7.0f}; fills in order, and {.length = 12.5f, .width = 7.0f} fills by name. Either works — pick one and stick with it.
Two ways to set the sides
Here are the two forms side by side. Both create the same rectangle. The first is shorter; the second is safer if the struct ever changes.
/* Fill in order */
struct rectangle a = {12.5f, 7.0f};
/* Fill by name (C99) */
struct rectangle b = {.length = 12.5f, .width = 7.0f};
Both end up with the same numbers. The first depends on the order of fields in the struct; the second doesn't. If you're typing code you'll come back to, use the named form — it costs a few extra characters and saves you an afternoon of debugging somewhere down the line.
| Style | Example | Good for | Watch out for |
|---|---|---|---|
| In order | {12.5f, 7.0f} |
Small structs, quick code | Breaks if the fields are reordered |
| By name | {.length = 12.5f, .width = 7.0f} |
Real projects, longer structs | Needs C99 or later (fine on modern compilers) |
Complete C Program
Program: fill a rectangle and print its dimensions
#include <stdio.h>
struct rectangle {
float length;
float width;
};
int main(void) {
struct rectangle r = {12.5f, 7.0f};
printf("Rectangle Dimensions\n");
printf("--------------------\n");
printf("Length : %.2f\n", r.length);
printf("Width : %.2f\n", r.width);
return 0;
}
And here's the same idea with names on the fields, plus a variant that reads the sides from the user:
Variant 1: named-field form
#include <stdio.h>
struct rectangle {
float length;
float width;
};
int main(void) {
struct rectangle r = {.length = 12.5f, .width = 7.0f};
printf("Rectangle: %.2f x %.2f\n", r.length, r.width);
return 0;
}
Variant 2: read the sides from the user
#include <stdio.h>
struct rectangle {
float length;
float width;
};
int main(void) {
struct rectangle r;
printf("Enter length: ");
scanf("%f", &r.length);
printf("Enter width: ");
scanf("%f", &r.width);
printf("\nRectangle Dimensions\n");
printf("--------------------\n");
printf("Length : %.2f\n", r.length);
printf("Width : %.2f\n", r.width);
return 0;
}
All three versions produce a rectangle with a length and a width. The first is the assignment answer. The second shows the named-field form. The third is what you'd actually write in a real program — ask the user, then display what they entered. The &r.length in the scanf is essential; without it, scanf writes to the wrong place and you get garbage.
Program Output
Rectangle Dimensions
--------------------
Length : 12.50
Width : 7.00
And for the user-input version:
Enter length: 8.75
Enter width: 3.5
Rectangle Dimensions
--------------------
Length : 8.75
Width : 3.50
Try a square — 5 5 — and both lines will show 5.00. Try decimals like 10.25 and 4.5, and you'll see that the program keeps the decimal places exactly as entered. The formatting is what makes it look like a real measurement instead of a wall of digits.
Step-by-Step Explanation
Short program, few moving parts. Here's what each line does.
1. The struct
Same struct rectangle as the last assignment — float length and float width. Nothing new. The type was settled in assignment 10.
2. Filling the rectangle
struct rectangle r = {12.5f, 7.0f}; is the whole initialization. The compiler copies 12.5 into r.length and 7.0 into r.width, in that order. The little f at the end of each number says "this is a float", which avoids a quiet narrowing conversion from double.
3. The named-field form
{.length = 12.5f, .width = 7.0f} is the same thing, just with field names on each value. The compiler matches each value to its field by name, so the order doesn't matter. You can write {.width = 7.0f, .length = 12.5f} and get the same rectangle. This is the safer habit, especially once a struct has more than two or three fields.
4. Reading from the user
scanf("%f", &r.length) reads a float and stores it directly in the length field. The & is the address-of operator — scanf needs to know where to put the value. &r.length is the address of the length field inside r, which is exactly what scanf wants. The same for &r.width.
5. Printing the dimensions
Two printf lines, one for each field. The %.2f shows exactly two decimal places, so 7.0 prints as 7.00 and 8.75 prints as 8.75. The labels ("Length :", "Width :") aren't required by the compiler, but they make the output read like an actual spec sheet rather than two loose numbers.
6. Why the formatting matters here
The next assignment computes area and perimeter from these dimensions. When you have four or five lines of output, having them line up nicely matters more. If "Length : 12.50" and "Width : 7.00" are formatted consistently, adding "Area : 87.50" and "Perimeter : 39.00" below them just works. If the labels are different widths or the decimals vary, the output gets harder to read. Formatting is a small thing, but it pays off the moment your output grows beyond a couple of lines.
7. Where this goes next
Assignment 12 computes area and perimeter from exactly this struct. r.length * r.width for area, 2 * (r.length + r.width) for perimeter. Everything else is printf formatting. So if you're comfortable with the fill-and-print pattern here, the next one is a short step — just a little arithmetic on the fields.
🎯 Remember this: fill with {12.5f, 7.0f} or {.length = 12.5f, .width = 7.0f}. Read from the user with scanf("%f", &r.length). Print with %.2f so the sides look like real measurements. The named-field form is safer if you might reorder the struct later.
Frequently Asked Questions
1. Can I fill the rectangle after declaring it?
Yes — struct rectangle r; r.length = 12.5f; r.width = 7.0f; does the same thing across three lines. The brace form only works at the point of declaration. If the values come from the user, you have to set the fields one by one with scanf, as in the third variant.
2. Does the order of values matter?
In the brace form without names, yes. The first value goes to length and the second to width, matching the field order in the struct. In the named form {.width = 7.0f, .length = 12.5f}, no — the names override the order. If you're going to write the initializer and might come back to it later, use names.
3. Why does 7.0 print as 7.00?
Because %.2f tells printf to always show two decimal places, even when the underlying value is a whole number. 7.0 becomes 7.00, 12.5 becomes 12.50, and 8.75 stays 8.75. The point is consistency — every length and width prints with two decimals, so the output looks like a proper spec sheet.
4. Do I need the f after 12.5?
It's a good habit. Without the f, C treats the value as a double and narrows it to float when storing. The result is almost always correct, but some compilers warn about the implicit conversion. Adding the f keeps things tidy and avoids the warning.
5. What if I enter 0 or a negative number?
The struct will accept it — nothing in C prevents a negative length. But a rectangle with a zero or negative side doesn't make geometric sense, and any area or perimeter computed from it will be zero or negative. In a real program, you'd check the input and reject it: if (r.length <= 0 || r.width <= 0) { error }. For this assignment, assuming positive values is fine; the validation habit is worth adding in the next program.
6. Can I display the dimensions on one line?
Yes — printf("Rectangle: %.2f x %.2f\n", r.length, r.width); prints them as a single line, which is compact and reads naturally. The multi-line form is better when you're going to add more lines of output (area, perimeter, diagonal). Both are valid; pick based on what looks cleaner in your program.
💡 Tip: After this, try adding a small check that prints "square" if r.length == r.width. Floating-point equality is a bit tricky (you might want fabs(r.length - r.width) < 0.0001), but for values the user types by hand, direct comparison is usually fine. Then try the area and perimeter in the next assignment — same struct, same fields, just a bit of multiplication.