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.
Syntax
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 */
}
Reference
How C Prints a Star
Map each part of the console shape to a C call or conversion.
Piece
C
What it does
Star
printf("*")
Writes * and stays on the row
One character
putchar('*')
Writes that byte, with no format string
Indent
printf(" ")
One space; call it in a counted loop
%d
printf("%d", '*' + 1)
Writes the int 43
%c
printf("%c", '*' + 1)
Writes +, the character at code 43
New row
printf("\n")
Ends the row
Edge only
putchar(col == 1 || col == width ? '*' : ' ')
A hollow row
Code
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;
}
Output
*
**
***
****
*****
How It Works
The outer loop walks row from 1 to 5.
printf("*") writes the star and stays on the row.
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;
}
Output
*****
****
***
**
*
How It Works
putchar('*') writes the character and stays on the row.
putchar('\n') ends the row, so a format string is not needed.
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;
}
Output
*
***
*****
*******
*********
How It Works
Row 1 prints 4 spaces and 1 star. Row 5 prints 0 spaces and 9 stars.
Each printf stays on the row. '*' + 1 would be the int 43, not a second star.
printf("\n") ends the row after both inner loops.
Programs
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.
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.