Preloader
C
  • Estimated reading time: 1 Minute

How to print a hollow square/box/rectangle pattern with asterisks or a custom character in the C language

How to print a hollow square/box/rectangle pattern with asterisks or a custom character in the C language

When I needed to do this on the university in the introduction to programming, achieving this task gave me a couple of extra points for the final note of the work of Loops.  In this article, we'll share with you a very simple script to create a hollow box/square output according to X number of asterisks on every side in the console with the C programming language:

#include<stdio.h>

void main()
{
    int number;
    
    printf("Provide the number of asterisks by side: \n");

    scanf("%d", &number);

    for (int i = 1; i <= number; i++) {
        for (int j = 1; j <= number; j++) {
            if (i == 1 || i == number || j == 1 || j == number){
                printf("* ");
            }else{
                printf("  ");
            }
        }

        printf("\n");
    }
}

The program will prompt for the number of asterisks that will be used for every side of the square and will print the output shown in the example at the beginning of the article.

Print pattern with custom character

In the following snippet, the user will be able to insert the custom character that will be used to draw the square:

#include<stdio.h>

void main()
{
    int number;
    char Ch;

    printf("Please Enter any Symbol\n");
    scanf("%c", &Ch);

    printf("Provide the number of asterisks by side: \n");

    scanf("%d", &number);

    for (int i = 1; i <= number; i++) {
        for (int j = 1; j <= number; j++) {
            if (i == 1 || i == number || j == 1 || j == number){
                printf("%c", Ch);
            }else{
                printf(" ");
            }
        }

        printf("\n");
    }
}

As with a custom pattern you may be not able to control the horizontal measure of every character, you may want to print only the character without extra spaces, otherwise the shape won't be exact.

Happy coding !

Share:
Carlos Delgado

Carlos Delgado

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Related articles
How to determine whether a number is strong in C
30 Mar, 2019
  • Estimated reading time: 2 Minutes
How to determine whether a number is an Armstrong number in C
25 Mar, 2019
  • Estimated reading time: 1 Minute
How to print the Floyd's Triangle in C
20 Mar, 2019
  • Estimated reading time: 1 Minute
Weekly trending
How Developers Are Building Smarter Blockchain Applications in 2026
16 Jun, 2026
  • Estimated reading time: 6 Minutes
The Best Sora Alternatives in 2026: Kling, Veo, and More Compared
16 Jun, 2026
  • Estimated reading time: 6 Minutes
How Smart Tech Helps Minimize Home Water Waste
16 Jun, 2026
  • Estimated reading time: 6 Minutes
Our Sponsors

Our blog is proudly supported by industry-leading sponsors.