r/cprogramming • u/BagelMakesDev • Dec 26 '25
How is my "postcard" code? Anything I can do to improve it?
./xmas.c
#include <stdio.h>
int treeWidth = 7;
void dots(int i) {
for(int x = 0; x < (treeWidth-i)/2; x++) {
printf(" ");
}
}
int main(int argc, char *argv[]) {
if (argc > 1) {
sscanf(argv[1], "%d", &treeWidth);
}
for (int i = 1; i <= treeWidth; i += 2) {
dots(i);
for(int x = 0; x < i; x++) {
printf("*");
}
printf("\n");
}
dots(1);
printf("|\n");
printf("* Merry Christmas, and a happy New Year! *\n");
}./xmas (output)
*
***
*****
*******
|
* Merry Christmas, and a happy New Year! *
16
Upvotes
4
u/BagelMakesDev Dec 26 '25
Updated code, based on u/zhivago's suggestions:
9
u/zhivago Dec 26 '25
1
u/Key_River7180 Dec 26 '25 edited Dec 27 '25
I prefer to use
printf()with %*s for these kinds of things (though it's kinda verbose). Anyways, your code is alright though:1
u/[deleted] May 20 '26
[removed] — view removed comment