C Star Pattern Programs

Beginner
6 min read
Updated: Nov 2026
3 examples
printf + putchar

Definition and Usage

A C star pattern is a console program that prints '*' in rows. The outer for loop chooses the row. The inner loop prints each star with printf, then printf("\n") ends that row.

'*' + 1 is an int. The char is promoted before the addition, so printf("%d", '*' + 1) writes 43. printf("%c", '*' + 1) writes +, the character whose code is 43, not another star. Adjacent string literals are joined at compile time, so "*" "*" is "**". "*" + "*" does not compile.

printf("*") stays on the row.

It writes the characters in the string and does not end the line. Call printf("\n") once after the inner loop. putchar('*') writes one character with no format string. C has no repeat function, so a solid row is still a counted loop.

C Syntax

The inner loop runs once per star. printf("*") keeps those stars on one line, and the newline comes after that loop.

C
for (int row = 1; row <= rows; row++) {
    for (int col = 1; col <= row; col++)
        printf("*");      /* same row */
    printf("\n");         /* end the row */
}

How C Prints a Star

Map each part of the console shape to a C call or conversion.

PieceCWhat it does
Starprintf("*")Writes * and stays on the row
One characterputchar('*')Writes that byte, with no format string
Indentprintf(" ")One space; call it in a counted loop
%dprintf("%d", '*' + 1)Writes the int 43
%cprintf("%c", '*' + 1)Writes +, the character at code 43
New rowprintf("\n")Ends the row
Edge onlyputchar(col == 1 || col == width ? '*' : ' ')A hollow row

C Examples

Three programs. Each one changes only the C that chooses how many stars to print, and where the spaces go.

Example 1 — One star per inner-loop pass

Row 1 prints one star. Each later row prints one more, because the inner loop stops at row.

C
#include <stdio.h>

int main(void) {
    int rows = 5;
    for (int row = 1; row <= rows; row++) {
        for (int col = 1; col <= row; col++)
            printf("*");
        printf("\n");
    }
    return 0;
}

How It Works

  1. The outer loop walks row from 1 to 5.
  2. printf("*") writes the star and stays on the row.
  3. printf("\n") after the inner loop starts the next row.

Example 2 — Write each star with putchar

Count row down from 5. putchar writes one character and does not look for a conversion.

C
#include <stdio.h>

int main(void) {
    int rows = 5;
    for (int row = rows; row >= 1; row--) {
        for (int col = 1; col <= row; col++)
            putchar('*');
        putchar('\n');
    }
    return 0;
}

How It Works

  1. putchar('*') writes the character and stays on the row.
  2. putchar('\n') ends the row, so a format string is not needed.
  3. row-- makes each later line one star shorter.

Example 3 — Spaces, then an odd run of stars

One loop prints the indent. A second loop prints 2 * row - 1 stars. C has no call that repeats a character for you.

C
#include <stdio.h>

int main(void) {
    int rows = 5;
    for (int row = 1; row <= rows; row++) {
        for (int space = 1; space <= rows - row; space++)
            printf(" ");
        for (int star = 1; star <= 2 * row - 1; star++)
            printf("*");
        printf("\n");
    }
    return 0;
}

How It Works

  1. Row 1 prints 4 spaces and 1 star. Row 5 prints 0 spaces and 9 stars.
  2. Each printf stays on the row. '*' + 1 would be the int 43, not a second star.
  3. printf("\n") ends the row after both inner loops.

11 C Star Pattern Programs

Each card is a full C program: nested for loops, printf("*") or putchar('*'), and a newline. Open one for the walkthrough.

Frequently Asked Questions

In C, '*' + 1 is an int. The char is promoted before the addition, so the sum is 43. The %d conversion writes those digits. printf("%c", '*' + 1) writes the character whose code is 43, which is +. printf("*") writes a star. "*" "*" is the string "**" because adjacent string literals are joined at compile time. "*" + "*" does not compile.
printf("*") stays on the same row. printf("\n") ends the row. Call it once after the inner loop. If the newline sits inside the inner loop, each star lands on its own line.
Yes. putchar('*') writes one character and does not parse a format string. putchar('\n') ends the row. C has no repeat function, so a solid row is still a counted loop.
Print rows - row spaces with printf(" "), then print 2 * row - 1 stars with printf("*"), then printf("\n"). Row 1 has one star; each later row adds two.

Next: Star Pattern 1

A full C walkthrough of the right-angled star triangle, from printf("*") through the newline.

Star Pattern 1 →

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.

28 people found this page helpful