Hollow Diamond Inside Square Star Pattern in C

Beginner
⏱️ 10 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
2n wide, 2n − 1 tall

What You'll Learn

You print a frame with a solid row of * on the first line and again on the last line, and mirror the rows in between so the middle row is narrowest (only the left and right border stars with a wide hollow gap). The width is 2 * rows; the number of lines is 2 * rows - 1.

This is not the same loop structure as Program 9: there the diamond stands alone with constant width 2 * rows - 1 per line. Here the layout matches the classic “diamond inside a box” picture when rows = 5.

⭐ Pattern Output

When you run the program with rows = 5:

Output
**********
****  ****
***    ***
**      **
*        *
**      **
***    ***
****  ****
**********
1

Complete C Program

Fixed rows = 5 version:

c
#include <stdio.h>

int main(void) {
    int rows = 5;
    int line, j;
    int height = 2 * rows - 1;
    int width = 2 * rows;

    for (line = 1; line <= height; ++line) {
        if (line == 1 || line == height) {
            for (j = 1; j <= width; ++j) {
                printf("*");
            }
        } else {
            int i = (line <= rows) ? line : (2 * rows - line);
            int left = rows - i + 1;
            int gap = 2 * (i - 1);

            for (j = 1; j <= left; ++j) {
                printf("*");
            }
            for (j = 1; j <= gap; ++j) {
                printf(" ");
            }
            for (j = 1; j <= left; ++j) {
                printf("*");
            }
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Grid size

width = 2 * rows, height = 2 * rows - 1. For rows = 5 you get 10 columns and 9 lines.

Dimensions
2

Top and bottom bars

When line == 1 or line == height, print width stars. Those are the full horizontal edges of the frame.

Border
3

Inner rows

Map line to i (distance from the tip): i = line when line <= rows, else i = 2 * rows - line. Then left = rows - i + 1, gap = 2 * (i - 1). Three inner passes: left stars, gap spaces, left stars.

Symmetry
4

Finish each line

printf("\n") runs after the full-width bars and after each inner triple, so every output row is exactly width = 2 * rows characters (10 when rows = 5).

println
=

Hollow center

The middle gap widens to the waist (line == rows), then mirrored lines tighten it. O(rows²) character writes, O(1) extra memory; 10-column lines scroll horizontally in the preview on phones.

2

Variation — User Input Version

Use scanf for rows:

c
#include <stdio.h>

int main(void) {
    int rows;
    int line, j;
    int height, width;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    height = 2 * rows - 1;
    width = 2 * rows;

    for (line = 1; line <= height; ++line) {
        if (line == 1 || line == height) {
            for (j = 1; j <= width; ++j) {
                printf("*");
            }
        } else {
            int i = (line <= rows) ? line : (2 * rows - line);
            int left = rows - i + 1;
            int gap = 2 * (i - 1);

            for (j = 1; j <= left; ++j) {
                printf("*");
            }
            for (j = 1; j <= gap; ++j) {
                printf(" ");
            }
            for (j = 1; j <= left; ++j) {
                printf("*");
            }
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Reject rows < 1 after input
  • Rewrite the inner part as one loop over j = 1width with boolean tests (border OR left block OR right block)
  • Compare side-by-side with Program 9 output for the same rows
  • Swap * for digits on the border only

Avoid

  • Mixing up 2 * rows - 1 (line count) with 2 * rows (line width)—both appear in this pattern
  • Copy-pasting i == j / i + j == rows + 1 snippets with j only running irows; that does not reproduce this 10-wide picture for rows = 5
  • Trailing extra space loops after the right star block (they break alignment)

Key Takeaways

1

Width 2n, height 2n - 1 for n = rows.

2

First and last lines: solid bars of *.

3

Other lines: left stars, gap spaces, left stars with i mirrored.

4

Not the same as Program 9’s diagonal-only loop pattern.

5

Time complexity O(n²).

❓ Frequently Asked Questions

Solid rows cap the top and bottom. On inner rows, two equal star runs sit on the left and right; the space between them is the hollow region. The run length and gap follow left and gap from the formulas above, with i mirrored below the waist row.
The width is 2 * rows, so 10 when rows = 5. The height is 2 * rows - 1 (9 lines), so the outer shape uses a wide horizontal bar and matching side columns on the inner rows.
Program 9 is a hollow diamond alone with uniform line width 2 * rows - 1. This program adds full-width top and bottom rows of length 2 * rows and builds each inner line from left block, gap, and right block.
O(n²) for n = rows: Θ(n) lines, each printing Θ(n) characters.

You’ve Reached the Last Numbered Star Pattern

Review Programs 9 and 10, then browse the hub for more C topics.

All Star Patterns →
Did you know?

Many older tutorials show a different C listing for this picture; the version here is checked so every line length matches the math: 2 * left + gap == 2 * rows on inner rows.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful